Roast your own.  Green, unroasted coffee beans maintain robust flavor for up to a year.  Use heat sources  such as a coffee-bean roaster, popcorn popper, or contertop rotisserie oven.  The amount of time you expose the beans to heat may vary, just be sure to roast them evenly.

Buy small amounts.  The moment they're done roasting, coffee beans begin to lose their freshness, which is why you should purchase smaller quantities, enough to last one or two weeks.

Get into the daily grind.  Leave beans whole until you're ready to brew.  Grinding too far in advance exposes coffee to air and moisture, which depletes flavor.

Store properly.  If you prefer to buy larger amounts, keep a few days' worth on hand and store the rest in any plastic, glass, or stainless-steel container, as long as it is airtight and watertight.  Store in a dark place, away from heat sources like stoves or sunlit windows.

Keep it on ice.  For storage linger than one week, keep coffee in an airtight container in the freezer, not the refrigerator.  Frozen coffee can stay fresh for more than a month, but be careful not to thaw and refreeze.  Instead, grind and brew beans while still frozen.