They can use something called Spit Bucket.
Actor Nick Offerman (Parks and Recreation) explains:
When you consume something in a scene, you learn to be very circumspect because you may end up having to consume that amount 30 times.
When you're young, you think, "Man, it would be really cool if I smoke in this scene." Then, on take 18, you're green and vomiting because you've been smoking for 90 minutes.
So you learn with food: If you have to take a bite of something, don't swallow it, and get a spit bucket.
Even if you're eating one bite of cake, you may end up eating four pieces of cake by the time you add them all up. It makes sense, especially if you're attractive. I have the good fortune of not being included in that category.
Julia Roberts revealed in an interview about her movie Eat Pray Love that she doesn't like a spit bucket:
Did you have a spit bucket by your side for all of the food scenes?
Well, first of all, that grosses me out. But the truth of the matter is, there probably would have come a point when I would have used it.
If you look at any of the scenes of eating, by the end of the scene, I’m done eating. Like in the scene with the pizza, by the time the scene is over, I’ve eaten the entire piece.
When we were in Naples, we started shooting at 8 in the morning, and I think by 8:45 I’d eaten 8 or 10 pieces of pizza. Pizza was what I ate all day that day.
What Are Actors Actually Snorting, Smoking And Drinking In Films?
Most of the time, it's not the actual substances...
Click the link to learn what substitutes they use for cocaine, marijuana and cigarettes (and for which movie Nicolas Cage snorted real cocaine)