It is best to buy your coffee in whole beans and grind it yourself at home right before brewing.
It is generally best to purchase coffee within 2 weeks of its roast date.
Speaking of roast dates it is best to buy coffee that has a roast date clearly visible on its packaging to ensure freshness.
You only need to buy enough coffee for a week or two at a time. When you are close to running out get more which also works to ensure the freshest cups possible.
Keep your coffee in an airtight environment. If your bag is resealable be sure to do so after every use. If the bag isn't be sure to transfer your coffee to a container that is.
Keep your coffee dry by avoiding humidity.
Store your coffee in a dark place since light can accelerate the speed at which your coffee stales. Avoid clear containers and direct sunlight.
Avoid putting your coffee in the refrigerator (we know you've all been told to try this...). Keeping your coffee in the refrigerator will not extend the life of it and runs the risk of your coffee absorbing the aromas of other items in your refrigerator.
If you must store your coffee for an extended period of time, a freezer and an airtight container will help slow down the staling. When it's time to use the coffee you will need to defrost the amount that you are using.
The darker the coffee has been roasted, the faster it will stale since dark coffee beans are more porous.