Headphone Jack Detection
Double check to make sure there is no dirt in the headphone port - there is at least one sensor there that might be dirty and making it hard for the system to tell what is or is not plugged into the port. Blow it out with compressed air and/or carefully remove whatever is in it with a toothpick.

http://www.instructables.com/id/How-To-Clean-A-HeadPhone-Jack/

In many Macs, the headphone jack also has optical audio output in the same port, so if some dirt is in there, the system might think an optical cable in plugged in, so the headphone circuit will be turned off. If the system preferences says "digital", than probably this is what has happened.