To add a bit more to Fenando’s answer your jack has a set of switches as you can see here
So when the plug is inserted the internal speaker connection is dropped. In addition the digital output is enabled which is the source of the Red light coming from the jack.
Sometimes the contacts get out of alignment or dirty which is what I suspect you are facing here.
At this point I think you’ll need to pull the logic board out to inspect it for corrosion or other liquid damage around the DAC chip which is located on the back side of the logic board (marked RealTek) MacBook Pro 17" Unibody Logic Board Replacement. You might need a new logic board.