There are 4 fundamentals to perfect cups of coffee. Freshness, proportion, water and grind.
So once you have opened your bag of coffee you should consume it within the week. When it's ground you should consume it within 24 hours. It's not reccomended to freeze coffee as that will put condensation in the coffee. The beans do better in airtight dark containers.
So you got the freshness down now you need to figure out the coffee to water ratio. When you have too much coffee in with the water it becomes muddled and can become over astringent. Too little coffee and it tastes weak and flavorless.
Now the water bit, since 99% of your coffee is water go ahead and get really nice purified water. At starbucks the water is tripple filtered and the filters themselves are changed frequently as well.
Lastly you need to have the grind right. This part is simple chemistry and you need to think about how quickly water runs through sand or how it runs through rocks. Depending on your filter and the shape of it or even if you do french presses you need to grind it just right so the water passes through the coffee at the proper speed to not make it muddled or weak tasting. This part really coordinates with proportion to keep the same issues at bay