Spices start losing their essential oils the moment they are ground or exposed to air, light, and moisture. Whole spices last far longer than ground ones because their surface area is smaller, so buy whole where you can and grind small batches as you need them.
Keep containers airtight, store them away from direct sunlight and the heat of the stove, and use a dry spoon every time to avoid introducing moisture.
Most ground spices are best used within six months, while a few whole spices such as cinnamon sticks and cloves can hold their character for a year or more when stored well.