Daan's answers above work for probably a majority of people with this problem, but if you're like me, and those fixes stop working after a while or don't work at all, it's possible to actually break the circuit that erroneously switches you to the Digital Output and prevent it from activating, leaving you with headphones and speakers and normal.
The solution, which originally comes from stin17 on this MacRumors thread, is intimidating but uncomplicated. We simply want to interrupt the line between the audio codec IC on the logic board and the little switch in the headphone jack by removing a component in between. This is usually a small resistor on older models, and after the Retina MBPs I believe it is an inductor.
If you can correctly identify your logic board model number (physically located on the logic board itself, usually a 6- or 7-digit number; for example, my 2012 MBP with a 2.3GHz i7 is a model A1286 and board number 820-3330-B) and find the boardview and schematic, you'll be able to find a resistor in line with the digital output switch, which, when removed, will prevent the audio codec IC from reading the digital switch. This permanently disables the optical digital/TOSLINK audio output, and you should not do this unless you are certain you've found the right resistor! However, if you're sure you know which components you're looking at, it takes about 10 seconds to scrape the tiny surface-mount resistor off the logic board with an x-acto knife.
If you have an A1286 macbook, it is likely R6805, which you can find in schematics and boardviews; this part number will certainly be different on other models. On my logic board, the circuit we want to interrupt is labeled AUD_PORTB_DET_L. Later models, after Retina I believe seem to rename this circuit to AUD_CONN_TIPDET_1 or AUD_CONN_TIPDET_2; I'd guess TIPDET_1 is headphone detection and TIPDET_2 is TOSLINK detection, but I'm not positive since I don't have one of those boards to check. Make sure you double check these on your particular model! I can't guarantee that R6805 is the one to get, but that's a good place to start.
The result here is that the digital switch inside the headphone jack remains physically stuck, but it no longer matters as it cannot complete its circuit to erroneously tell the audio IC that there is a TOSLINK plug in the jack. Again, optical digital audio output over TOSLINK does not work after doing this, unless you replace the resistor and go back to the current "digital out" problem, but it sounds like you don't need optical digital audio output. This is straightforward and familiar for some, for others it's new and terrifying! I started learning my way around schematics and boardviews because of this exact problem. If you feel up to the task and you don't need TOSLINK audio output from your headphone jack, this is the most reliable and elegant fix when the other temporary methods fail.