The reason why cats purr is unknown, although the pleasant noise is associated with pleasure and comfort. However, the question "Why do cats purr?" is really not such a simple one, since they also purr when they are sick or afraid. In these cases, cats purr to comfort themselves.
Purring can also be an appeasement gesture. When animals do not want other animals or humans to hurt them in self-defense and want to show the potential predator that it is harmless and not worth attacking, the animal will show some kind of appeasement gesture. Among dogs, this takes the form of submissive movements, such as bowing the head. Cats purr to show the observer that the cat is really kitten-like and will not bite them (although it is not unheard of for cats to attack after they finish a round of purring).
In adult cats, purring is heard in domesticated cats when they are petted, and in feral cats during sexual or other social contexts (but I've heard on many ocassions stray cats purr when you pet them too).