It's pretty easy if you have an audio jack like this one, basically there are switches on the contacts in addition to the normal signal. Once you know when something is plugged in to the Line In there are a quite a few different ways to stop the signal from going to the headphones. It can be done using hardware, or if you have some sort of microcontroller/computer involved software as well.