It's a normal developmental stage, he doesn't know how to communicate to you what he wants.
I would have kept the drink and snack to the side and waited for him to ask for it. At that age they are fairly quick to tell you if they are hungry or thirsty IME. If he wanted to play instead what was the problem? If it's mealtime or you think he's getting over hungry it's a good idea to plan to leave in plenty of time. However 18mos don't really understand leaving something they are still enjoying now because you know they will get hungry or overtired soon. So you can't explain it like that but you can present it as a kind of done deal ie it's simply time to leave now without any particular reason (meal or nap) attached. But hunger and tiredness are definite exacerbators of tantrums so it is well worth staying aware of rough expectations for these and trying to stick to usual nap and mealtimes and if you know he's skipped some then maybe keep activities low key.
Again it can be difficult for them to switch gears especially when you need them to stop something fun like playing for something boring like sitting in the car or walking home. So one trick which can help you here is giving them some warning and finding something positive/exciting about the next activity to focus on.
Bear in mind at this age they do not understand times, so telling him you will leave in five minutes will do nothing to avoid the tantrum and might just make him wary of the phrase five minutes (I remember my brother throwing an epic one when my stepmum said "Yes you can play football for five minutes" because he thought he was having to go to bed!) Instead tell him in events. So "You have time for three more goes on the slide, then it's time to go." or when a bit older perhaps something like "You have time for three more slides, or one more go on the car. Which would you like to choose?" If you have three goes on the slide, count with him, as he probably doesn't know numbers yet, remind him when it's the last one, perhaps even say bye bye slide see you soon. It's all about giving clear markers, so any bit of language or hand gesture he knows, incorporate that. Very very matter of fact and if he's creating a scene just pick him up and move him out of the way of others until he has calmed down. You can empathise with him if you like saying oh, you're sad it's time to go, you wish you could stay here forever. Or you can ignore it, it depends how you would like to deal with the tantrum. Don't panic or feel anxious about what others might think or that you have to stop the tantrum ASAP - he will get through it when he gets through it and every other parent will have experienced a tantrum at some point!
Then once you've got him to switch gears and understand it's time to go (whether he's happy about it or not) bring in something exciting or positive about the next activity, like DS, how many leaves can we jump on on the way home? Or Let's see how many red cars we can spot. Or What would you like for lunch? If he leaves calmly, if you like you could praise this and say well done for doing as Mummy says and/or offer some reward, just be sure to explain what it's for.