What's the specific cause of the headphone/built-in speaker switch?
option click on the speaker icon in the menu bar. There you can select the output:



The screenshot doesn't list a headphone, because I don't have a pair on me at the moment..

Alternatively go to System Preferences --> Sound --> Output and select you prefered output: