The most widespread belief is that they do this to purge themselves, because of a malaise stomach, but the truth is that the evidence is not conclusive. In fact, not all dogs vomit after eating grass.
However, there are many other hypotheses:
It is a behaviour inherited from their ancestors the wolves: the vegetal matter that has been eaten by their herbivorous prey may play a fundamental nutritive role (for example the contribution of fibre).
A study made by the University of California concluded that this was a natural behavior and generally it was not associated with any disease or discomfort, although they did see that the vomit was more common in dogs that showed symptoms of disease before eating grass. This study also found no evidence that they eat grass to cover a nutritional deficiency. It was concluded that it may be a behavior that reflects an innate predisposition inherited from wild canids. In nature, other animals such as chimpanzees eat specific leaves that promote intestinal motility and drag possible parasitic worms. They also observed that young dogs ate grass more often than older dogs, perhaps to deworm themselves, as younger dogs may have lower immunity to intestinal parasites.
In another study scientists saw that the studied dogs ingested more grass before eating their food, and as the day went on they ate less grass, perhaps because they may see it as a food source.
Other authors argue that they do so because maybe they like the taste, or perhaps even the relaxing effect of sniffing, selecting sprouts and chewing.