Remember Bill the Cat? He was a cartoon cat who was forever barfing on the rug. Felix or Sylvester would never be caught dead doing such a thing. However, as any cat owner knows, throwing up is something cats actually do frequently. It can be somewhat distressing, especially if you have nice rugs.
If you own a cat, you may have noticed something else they do frequently: they eat your houseplants. This is also rather distressing. If your cat is an outdoor cat, you've probably seen it eating grass.
From a cat's point of view, however, all of this is business as usual. If your cat vomits repeatedly or makes distressed sounds when vomiting, it is probably sick and should be taken to a vet immediately. However, an occasional barf is not unusual for a cat, and is actually a normal part of its routine. Cats swallow plants and grass, in fact, partly to induce vomiting. It probably isn't half as upset as you are!
So, why do cats barf? Oftentimes it's not related to food they have eaten at all. Rather, it's because they groom themselves by licking. They use their sandpapery tongues to remove dirt and loose hair from their fur.
All this unwanted material is swallowed during the cleaning process. Over time, undigestible clumps of hair form in their stomachs. Those clumps need to be removed, and bringing them up in a hairball is a quick and dirty way to get the job done.