Apple - Soft switch for Mac sound output
In Preferences->Sound, select "Show volume in menu bar". You should then see a little speaker icon in your menu bar.
If you option-click that (click while holding alt/⌥), you can select the audio output right there.
Update: Only works for audio devices connected via USB or DisplayPort/Thunderbolt, not for the Headphone port.
That can not be done. There is a hardware switch in the headset plug.
It will disable the build in speakers so no software can fix that.
Apple decided that when you plug in the headset you do not want your speakers going at same time.
That is why it is not possible to use software switch.