Well, from what I understand, it’s an evolutionary thing.
Back when food was not so easily accessable, we ate as much as we could when we could, because there was no promise of having another ‘meal’ for a long time. So things that had alot of energy value, like fats, sugars, we tried to eat more of, in order to stay alive and have a way to survive times when food was scarce.
Being omnivores, we ate plants as well, and eating a poisonous plant was potentially lethal. Over time, our bodies learned to identify those potentially harmful substaces, (usually basic (remember chemistry?) in nature) and developed a way to warn us before we ate them. Acids were similar, but because they were not as toxic, and were in some cases benevolent for us, they were given a different identifier.
Spicy, however, is a case of our taste buds actually burning, and the sensation if them dying is percieved as spicy/hot.
So, sugars=energy boost=sweet
fats=long term energy
so they tasted ‘good’ since they are most important. On the other hand…
acids=middle ground, but potentially lethal=sour
bases/poisons=lethal=bitter
spicy=burning taste buds=hot
Hope this answer helps.