When your cat may purr:
Behaviorists believe that the original purpose of purring was for a mother and kitten to communicate with each other. Kittens are born blind and deaf, but a mother cat can communicate with her young through the vibrations of her purrs. Because they cannot meow while nursing, a kitten might purr to tell his mother "all is well." Purring also works as a bonding mechanism between a kitten and its mother, as the mother may purr back in reassurance.
Purring behavior learned as a kitten carries into adulthood and this may be why your cat purrs while being petted — he's letting you know "all is well."
Adult cats purr when approaching other cats to let them know they are friendly.
Cats purr when they are afraid or distressed, possibly as a way of reassuring themselves.
A cat's smile: Many people have likened a cat's purr to a human's smile which is a good analogy. People smile for many emotions: happiness, nervousness, fear. Just like a smile, purring may indicate various emotions and doesn't necessarily always indicate happiness.
Fun fact: Many cats stop purring at the sound of running water. Your veterinarian may use this trick in the exam room in order to get a better sound of your cat's heartbeat which can be difficult to hear while your cat is purring.
Drs. Foster & Smith Educational Staff. "Why Do Cats Purr?" Why Do Cats Purr? 3 Reasons Kittens & Cats Purr. N.p., n.d. Web. 15 Jul. 2013.
"Why And How Do Cats Purr?" Everyday Mysteries: Fun Science Facts from The Library of Congress, n.d. Web. 2 Aug. 2013.