You've kind of answered your own question. If dogs eat something that doesn't agree with them, the best (and quickest) way they know for them to get it out of their body is just to sick it back out. And they know they can make themselves sick by eating grass.