It really depends on the spice. Dried chilli, for instance, will keep for much longer than saffron but generally spices are edible for years. This is a bit of a red herring however, for once ground/processed spices will rapidly lose their colour and flavour. I would say buy them whole when you can, for instance but whole corainder seeds, cumin seeds etc. then grind them as you need them in a pestle and mortar for small quantities or, as @pdworkin says a coffee grinder for larger amounts. This way they will retain their flavour for longer.
Also bear in mind that whilst the spices may stay edible for a long time, the insects and other such things that may have contaminated them in the time they’ve been sitting in a box in the cupboard may not be, so if you are considering using anything that’s been at the back of the shelf since time immemorial check it thoroughly for creepy crawlies/bits of brad or other foodstuffs that may have gotten into the box and gone bad.
xx