Purring is caused by the vibration of vocal cords due to neurological stimulation from brain activity.
The purpose is unknown, but it does seem to be associated with pleasurable activity. Cats can also purr when they are sick or hurt.
Purring also reinforces human response; people like hearing their cat purr so they pet it more. Cats usually purr when they are with someone they like or experiencing a pleasant sensation.