When you switch audio outputs, the volume level reverts to whatever level it was at when you last used that output. That means that muting the internal speaker before plugging in headphones should be enough to stop audio from playing through the speaker if you disconnect your headphones.
If you want to keep the speaker disabled even if you hit any volume controls, you can use Soundflower. It's an extension designed to let your Mac record its own audio output, which it does by creating virtual input and output options that feed directly into each other. That means that you can hold down Option, click on the volume icon in the menu bar, and select one of the Soundflower options as your audio output before plugging in your headphones and selecting them as the output device. If your headphones get disconnected, your audio output will default to Soundflower, which doesn't send anything through the speaker. To re-enable the speaker, just Option-click on the volume icon in the menu bar and choose the built-in speaker for output.