It can depend a bit on the plant, but at least half of the answer is that earlier on in the plant's existence, humans discovered that it tasted fairly nice and then started a programme of selective breeding, choosing only the tastiest plants to carry on for the next generation. So the advantage is often to us, and not the plant (or animal, as a similar procedure accounts for cows, sheep, pigs etc, that these days can sometimes be little more than living meat/ milk factories).
The other half of the answer is that the taste itself may sometimes be an incidental result of something that is beneficial to the plant -- in the same sort of way that eggs are tasty because they are just packed full of nutrition for a growing chick embryo. Alternatively, the taste can come from a chemical that is poisonous to certain animals that were natural predators, but is in too small doses to be a threat to us.
I suppose the short answer, then, is that the taste isn't important to the plant, but what causes it might be.