When it comes to flavor, there are really two things going on: how we perceive the taste, and the chemical compounds that we are actually detecting. Evolution has affected both.
First off, we are descended from ancestors who were superior at finding and consuming nutrients necessary to live. (Those that were less good by definition were less good at surviving, and therefore less good at having descendants.) One of the things that helps an organism determine what is worth eating or not is, of course, the flavor. In general, we tend to favor things like sweetness, fattiness, and saltiness. This makes sense. Sweet and fat things contain lots of energy, and in natural environments salt is actually a fairly critical mineral. We also like tart flavors, likely because vitamin C is also important. So, in a sense, we have evolved to favor things that are high in energy, because those energy reserves made our ancestors more successful at breeding...us! We also have strong aversions to certain things, like bitter flavors. This is again likely due to evolution. Many bitter flavors are the result of alkaloids, which are chemicals that can be toxic depending on dose. Sensitivity to those flavors lets us avoid them, and in theory gives us more of a chance to not get poisoned. (This is also one of the reasons that nausea is so powerful. We tend to strongly avoid anything we've eaten that was later associated with nausea and vomiting. This can help us avoid potentially fatal foods...)
As it turns out, many plants have evolved to take advantage of the fact that other organisms like to eat them. Many fruits stock their tissues with sweet sugars. Other animals then eat them for those sweet, energy rich sugars, and eventually poop out some of the seeds from the fruit. The plant gets its seeds distributed (in fertilizer, no less!) and in exchange the animal gets energy. All that we humans have done is purify various plants that store lots of sugar in their tissues in order to make candy, which is essentially hyperfruit: extra sweet and therefore delicious.
On the other hand, some plants want to avoid being eaten...especially parts of a plant that are important for making energy from light (leaves) or for storing energy (roots.) As a defense mechanism, many plants produce toxins in these tissues. Animals that eat those toxins either die or become ill enough that they avoid them in the future. The plants survives to make more plants, and the pattern continues.
The interesting part comes now, after thousands of years of agriculture and breeding. We have artificially created crops, animals, and foods that now taste extra good to us...and are in some cases eating ourselves into poor health. It turns out there can be too much of a good thing!