We're not cats so we don't know why they do it, but the Wikipedia page on cats (and purring) has some interesting stuff. They definitely purr when they're happy and around people they trust, so a purry cat is a happy cat. However, some cats also purr when they're very nervous. However, for that purr to be heard, they still have to be around someone -- often their owner, who they recognize, or if they're not around their owner they're around someone they hope they can trust. I've never heard a panicky cat purr if it's not around it's owner, though.
One theory is more about when cats don't purr. Wildcats, when resting, will often sleep/relax together in a pile, and will keep a low-level purr. When one of them notices something, usually food, it will stop purring, and the other cats will all almost instantly follow suit. In other words, they're able to alert each other to something without making a noise or movement.
Our kitten purrs when he's excited to see us or when he's close by, unless he's feeling playful. When he's in crazy cat mode, he doesn't purr. I figure it's because he's in attack mode, and doesn't feel like he needs to express his trust in anything at that point.