In addition to what Spinoral has said, and in response to your comment above, I will add a bit more about the mechanisms.
Essentially, in some form or another it's receptive fields all the way up.
A brief outline of the anatomy:
The cells from the spinal cord are pseudounipolar and have their cell bodies in the dorsal root ganglia in the spinal cord. The other "leg" of the sensory neuron projects to the medulla, which has cell bodies which send axons to the VPL thalamus, which then has cell bodies that project to the primary somatosensory cortex. There are points of decussation (crossing over the midline), but I'll ignore those for now.
The receptive fields:
The receptive fields of somatosensory neurons share much in common with the receptive fields of visual neurons. As for visual neurons, the somatosensory receptive fields comprise a restricted 2-dimensional region of space where a stimulus can evoke a neuronal response. In somatosensory neurons, however, space refers to a region of the body and the stimulus can be touch, vibration, temperature or pain
Essentially, the somatosensory input is "filtered" at all of those stops along the way and refined in terms of things such as whether the sensation came from the center or the edges of the receptive field, etc.
Cells in the cortex (the waypoint for the sensory information) are organized in microcolumns, which are representative of a particular location on the body (and arranged topographically into the "homunculus", as the other answer mentions).
All of this follows what is called the "labeled line" theory, which is getting a bit outmoded due to new knowledge about ensemble coding in neurons, but for purposes of the somatosensory system it still holds up pretty well.