How does an audio jack detect when a speaker is plugged in?
Most all jacks (DC power or audio) have an extra terminal that is switched on plug insertion.  This is a mechanical switch.  Some designs use this extra terminal to switch between battery and external power (in the case of DC jacks) or to switch between headphones and speakers (in the case of audio jacks).

In the photo of this DC jack on Digikey you can almost get a good view of the internals.  The two lugs on the back of the jack are for the two DC power connections.  The third lug on the bottom is the switch input.  Audio jacks have a similar arrangement.