A car battery can usually last from 1 to 4 years without the car being started or driven.
Batteries that have never been used in the store can last for around 4 years before they go bad and if the battery is not sold within 4 years or so then the battery is usually sent back to the factory for refund to the store and then recycled in some cases.
But usually most car batteries in stores are sold before they expire and it's a good idea to check the date on the batter you buy to be sure it's not very old.
If your car will start you should start the car at least once a month even if not driving it and let it idle for 10 to 20 minutes to help keep the battery charged up and to help keep the fuel injectors and fuel lines clean and keep the gasoline from varnishing up and going bad.