Science seems to be good at answering how things came to be, but not so good at the whys. What is the underlying purpose behind the malarial parasite or some of the nasty genetic diseases (such as cystic fibrosis) that still afflict mankind? Or even, on a mundane level, why does a fruit - the chilli pepper - seem to make itself as unpalatable as possible to all except gastronomic masochists? The facile response is to shrug one's shoulders and reply 'because that's the way the world is'.
Rather, that is the way the world has evolved: if there is an underlying purpose to any trait or feature of an organism it is generally because it gives some advantage over its competitors. People with CF, although they might not live as long as the rest of us, seem to be more resistant to certain strains of bugs. Similarly, sickle cell anaemia, a nasty disease which afflicts people of African extraction, confers complete immunity to malaria. Expediency rules, no more so than in the realm of Darwinism.
When an organism has a very pronounced trait, we are therefore forced to ask ourselves not why, but what evolutionary purpose does it serve? It had been suspected for a long time that chilli peppers had to have a very good reason for their ability to set the mouth on fire. Chillies, as any cook knows, are extremely strong-tasting1. However, they share that trait with a number of other plants. In these other species, it's pretty plain why they taste so bad: it stops animals from eating them. Some even go to the lengths of poisoning their devourers: deadly nightshade and the thorn apple, both related to chillies, contain extremely potent and nasty alkaloids. However, chillies are fruit and hence need to be eaten in order to disperse their seeds effectively. Why go to all the trouble of turning red to attract fruit-eaters if you're going to give them the shock of a lifetime? There doesn't really seem much point to this trait whatsoever.
At least, this is how it seems if you're a mammal. If human beings had evolved from birds, however, we wouldn't have been perplexed by this question at all, simply because we wouldn't have noticed that chillies were hot. Birds are completely insensate to chilli peppers: they can eat as many as they want with no ill-effects whatsoever2. What's more, it might even have a beneficial effect for them, killing off harmful bacteria in their gut.
So how does this benefit the chilli plant? Well, if your fruit gets eaten and then excreted by a bird, the seeds get spread further. Greater dispersal means a higher chance of seeds finding a beneficial environment in which to germinate and grow, which means better production of fruit, which means more chilli plants. So chillies make themselves distasteful to mammals only, and in doing so, get to prosper better than if they didn't. As for appealing to the only known animal with a masochistic streak3, that's another question entirely.