A cat purrs by a special mechanism in the cat's brain, which sends a signal to the muscles in the cat's voice box. This signal vibrates the muscles in the voice box, and the muscles act as a valve for air passing the voice box. This creates a continuous purr.
Cat purr for a variety of reasons, including when they are happy, when they want something or when they are hungry. Cats purr when they are in pain, as a way of soothing the pain. Kittens also purr a few days after birth as a way to bond with their mothers.