I had this problem and tried a bunch of fixes that didn't work like:
- un-plug and re-plug headphones repeatedly
- re-start phone
- clear out the jack of debris (there was none but I tried anyway)
90% of the time this is a hardware problem and not a software problem, as the main reason your phone thinks earbuds are in is because there is some sort of debris, rust, or other obstruction tricking your phone into thinking that earbuds are plugged in.
I read on other forums that it could be water damage in the headphone jack (but wait, I take amazing care of my expensive phone, how could this be?! Well if you are an athlete and train with your phone, it is likely that sweat trailed down your headphone cable and seeped into the jack over time. This is likely what happened in my case, and could be your problem too.
Not to fear. Go pick up a can of this stuff Quick Dry Electronic Clnr 11oz Can 12Pk - Walmart.com
CRC QD electronics contact cleaner is probably one of the most useful cleansers I've ever purchased. This $5 can has repaired hundreds, if not thousands of dollars worth of sensitive music, computer and tech equipment. It is non-conducting, quick drying and just all around awesome.
Put a half second spray of this stuff into your jack (phone can be on or off, doesn't matter because the solution is non-conductive as said before) and then put your earbuds in and out, and twist the plug in the jack. Do this for about 30 seconds.
This stuff should dissolve any rust on the contacts that could be shorting the circuit and tricking your phone into thinking that the earbuds are plugged in, and put a protective coating on the contacts as it dries to prevent more rust from forming.
This method should work immediately - as soon as the cleanser is dried, which should be seconds. If it doesn't work immediately, fear not. Go to bed. It should work in the morning, sometimes the cleanser just takes a little extra time (what happened in my case).
In the case that this does not work, you might need to replace your headphone jack as you can only repair rusted equipment so much, but I would only recommend doing this if your phone is no longer under warranty.
Hope this Helped.