It all begins with an stimulus (petting the cat, for example), which triggers the animal's Nervous System. The NS then sends a stimulus to the larynx muscles, which begin to contract and relax every 40 millionths of a second - approximately the speed of a hummingbird's wing-beat -, creating a vibration in the cat's throat and that particular sound of a little engine we all love so much.
It is also known that cats can use that sound to manipulate us humans to get food, attention and affection but, come on, did you really think that cats could be selfless?