Hmmm...odd. I plug a pair of headphones into the headphone socket on the front of my mac, in this case I have a 2010 Cheese Grater Mac and heaphones show up as one of the audio outputs. Under Audio MIDI Setup, it shows up as built in Output. Maybe the jack is stuck. you could use a toothpick to wiggle back and forth. Try plugging in and unplugging your headphone plug a number of times. You could also fire up Activity Monitor and look for coreaudioiod. Terminate the process and it should restart automatically. Other than doing a reset smc/pmu and zapping pram, that's all I could scare up this early in the morning. How to do the reset smc/pmu: IF your imac does not have the T2 chip in it, eg it's not a recent one, remove the power cable from the back, hold down the power button for about 30 seconds. let go of the button, plug in the cable, wait another 30 seconds and turn it on. for pram zap, on restarting, turning on, hold down Apple/Command-Option/alt-p-r keys together for 3 chimes then let go. IF your imac has the t2 chip, it's still simple: shut down your imac, unplug the power cord, wait 15 seconds, plug the power cord in, wait 10 seconds, and press power button. for the pram zap, do what I mentioned before, but you won't hear any chimes, instead the Apple logo will appear and disappear twice...
I don't know exactly what year of iMac 27" you have, but on some older models, there was an optical port in there as well as the headphones. that port has been removed. If you look at the headphone socket while the mac is running and you see a red light coming from it, that's it. you may have to wiggle the sides of the internal headphone socket to get the light to go out, or plug in and unplug the headphone jack a number of times.
Should be automatic.... but if not.... and if doing all the previous stuff did nothing, then you might have to go buy a USB audio dongle, plug that in the back and then plug your headphones into it
good luck
john b