It's hard to hang around specialty coffee for very long without hearing that "fresh" is important - and we'd like to take a few to talk through what that really means, and how you can use that.
Green coffee keeps for between one and two years after its harvest, keeping best and maintaining its flavour the most with relatively low per-bean humidity, somewhere around 10% seems to be optimal. We make our green buying decisions aiming to have used & replaced our green stock within about eight months, so that we can take care of this aspect of freshness for you.
How long roasted coffee is "good" for can be a somewhat murky and complex question: "fresh" is not quite the same as "still safe" in the way of other food products; so "best before" dates can be a little misleading.
In this context, "stale" is the point at which most of the distinctiveness of the coffee has been lost, this is mostly due to a combination of dissipation or evaporation of trapped gasses and volatile aromatics, and oxidation reactions in adelhydes and esters present in the freshly-roasted bean. As these degrade and/or evaporate, other chemical reactions in the sulphur-based compounds that provide many of the notes perceived as "richness" "buttery" or "honey" are replacing those notes with a dull and almost dusty 'stale coffee' note, the presence of furfurylmercaptan, which in small amounts is reported as "coffee taste" but in larger amounts is perceived as "stale" taste.
Based on our own testing, whole bean coffee stays fresh for between three to four weeks. However, it starts staling the moment it's exposed to oxygen, so "immediately" after roasting finishes - some people did notice stale notes as early as two weeks; but the largest volume of reported decline was between three and four or four and five weeks. By five weeks, almost everyone we had testing reported some decline compared to the previous week.
Because so much of what makes coffee taste "fresh" is so delicate and particularly vulnerable to open air, it's almost tough to overstate the role the bean itself plays in protecting the flavour locked inside. It's own cellulose structure protects its contents, either barring or slowing the progress of oxygen; this is part of why green coffee keeps so much longer than roasted: it's far harder. In addition, many of the vulnerable volatiles are held either within carbohydrate or protein structures in the bean, and others are similarly protected within the antioxidant lipids of the bean as well.
Grinding your coffee dramatically reduces its shelf life; by greatly increasing surface area for air to access the beans' contents, and by disrupting many of the structural protections of the bean itself as well.
Lifespan for ground coffee varies heavily by grind size; coarse grind may keep overnight (12H, say) while espresso-fineness will last between thirty minutes and an hour. It gets widely reported to trainee barista that ground espresso is only good for thirty seconds; while not strictly true it is still a very useful rule to keep them on task and away from building bad habits like idling on pre-packed shots. As an example, the 2015 US Barista Championship was won by Mr Charles Babinski, whose winning service included pre-ground espresso, done during prep about an hour prior.
Pre-ground absolutely works for a ton of people, or when there's not time & space for a grinder in your life - but coffee does stay far nicer far longer if you can keep it intact right up until you want to brew it.
There are absolutely a few ways to extend the lifespan of coffee, though. Heat, humidity, airflow, and direct light all accelerate staling - so keeping your coffee in a cool, dry, and dark place all help prolong its freshness.
Freezing coffee can be a contentious topic in coffee circles, but can help in very specific settings. If you're going to freeze, place the beans in an airtight bag - for our beans, place them, in the bag it came in, into a resealable freezer bag - squeeze as much air out as possible, and only freeze it once. If the frozen beans are taken out of the freezer and put back in, condensation will form on the beans and in the bag, dramatically affecting future freshness. Once you take them out, remove them from the airtight bag, and let stand partially open for a while, to allow the condensation to dissipate.
Some preground coffee's lifespan can be extended by "nitrogen flushing" the package; by filling the container of ground coffee with nitrogen the oxygen-bearing 'air' is displaced, substantially reducing the amount of oxygen in the parcel to stale the grounds. This method will allow a sealed package of ground coffee to last up to two months or so, at an immediate cost of about 90% of the initial quality, while bean and fresh.
Specialized containers are sold for storing coffee, but for the most part, only end up adding a few days at most in my experience. They assume that your coffee is fresh enough to still be venting CO2, and by sealing the container with a one-way valve the CO2 will displace the normal air out the valve, slowing staling similarly to nitrogen flushing - but with far less pressure to displace the ambient air.
Both the specialized containers and nitrogen flushing are a lot like freezing in their primary downfall: it only works once. After the container is opened, normal air rushes back in and either the sealing or the nitrogen flushing are lost, allowing the normal 'staling' timer to begin from that point.