What is pain?

I have often wondered why we have pain the way we do.  Why does it hurt?  Yes I know because it is hot, freezing, or sharp.  But it is the sensation that I am talking about.  Pain obviously had to evolve but when did animals first start to feel it.  There may have been a species of Homo that could touch fire without any pain living parallel to a species that could.  Natural selection would have soon rid the former from existence, that is if they existed in the first place.

This is what baffles me about science, but also fills me with an urge to learn to find out.  I am not a scientist, but I have a strong yearning to understand the world around me as best I can. This example of pain is just one of many things I am baffled about.