The nerves from whatever body part you are using lets the brain know that you should pull away from that object otherwise burning yourself
Chactation:
Fire has a large amount of thermal energy, when it comes into contact with your relatively cold hand, it's thermal energy quickly flows into it. The nerves in that area absorb that energy, transform it into a weak electrical signal, so your neurons can understand the information. The information is processed by the neurons, and then they decide on a logical reaction -pulling away from the intense heat-. That's my guess to how it happens. This all happens in a fraction of a second.
Chat with our AI personalities
You have receptors that are part of the nervous system that are just for hot or for cold. These are called thermoreceptors.