The following appears to have solved this specific problem (Music playing from iPhone speaker when AirPod removed from ear).
- Open Music
- Click on the current song at the bottom
- Click on the icon with three bullet points and three lines near the lower right (not the three dots near the top, on an iPhone 8 plus running iOS 16 this is the one near the bottom on the right)
- Look for the for the infinity symbol next to "Playing Next"
- Turn it off (ensure it has a transparent background rather than a white background)
- Clear your entire "Playing Next" playlist. This step is necessary and missing from other explanations I found online, and may solve this problem even if you did not have the infinity symbol turned on.
You may be able to do something similar on your MacBook, specifically clearing the "Up Next" or "Playing Next" playlist. I have not tried, I only had this problem on my iPhone. I am not sure if this solves the unbelievably annoying "feature" where music starts playing every time I plug my iPhone into my car audio system, but I hope it will.