The audio ports detect that something has been plugged in by monitoring for a change in resistance across the port. When nothing is plugged in the resistance is infinite, but when a coil is plugged in (speaker or mic) there is now a measurable resistance, and the computer pops up and asks you what it is you plugged in.
EDIT: I've been digging, and instead of trying to figure out what the audio adapter manufacturer's are doing, I decided to look at the jack manufacturer's to figure out if there are internal mechanical switches, as well as perhaps suggested uses.
Turning to Digi-key, I found that there are a few different mechanical ways the jacks provide for detection. The most common DOES seem to be a separate, internal switch circuit based on the position of the inner-most blade in the barrel; and they are only a couple cents more per unit than a non-switched jack. Here's a dual barrel surface mount by Tyco that I think looks similar to what can be on a motherboard.
Interestingly on many (most?) the detection switch is actually reversed. So the switch is closed until the plug is inserted, here's one by CUI.
This one by Schurter is crazy; it's a "stereo" jack that all three rings (including the ground) are switched. So it could tell you how deep the plug is pushed I guess ;) Only $6.50 each!
Using these switches DOES seem to be the more likely way they're doing it, and what I've been told in the past is probably incorrect, or at least not correct across-board.
I'm going to disect one Monday -- I gotta see if that switch is at least in the jacks Intel's been using. :)
If you are aiming to have the same sound on both headphones and speakers, then you may want to look into getting an audio jack y-adapter (splitter):
If you want different sounds on each a headphone and a speaker set then you're probably dependent on the abilities of the software mixer provided by your audio adapter's manufacturer, and/or the software you're using.
Depending on your situation, you may want to consider getting a secondary audio card as well, that way you can point different applications at each audio device.