I bought the 27" IMAC in 2013. It started to shut off by itself, but never before it had been on for at least 4 hours. Then the ON button would not work unless I unplugged the power cord, waited a minute or so, then replugged it. Then it would reboot OK after pushing the ON button.
Many days it would never shut off. It seemed to shut off more often when my room was warm (upper 70s, 80) during the summer. I called Apple support and she suggested to basically do a fresh install of the OS, etc. I did not want to do that since I felt the problem was heat related.
Then for months my iMac did not shut off. But the other day it started doing it again. Then I unplugged the power, replugged, then it rebooted OK.
But I just "may" have figured out my problem. I hope it helps others here.
Ever since I bought my IMAC I have never heard the fan operate and the entire back of the metal monitor, near the vent and also even the metal stand were very warm to the touch. Way too warm IMHO. After searching Google for "IMAC fan does not work", I came across this free software app:
http://www.macupdate.com/app/mac/47386/macs-fan-control
It will show your fan speed and also sensor temperatures (Ambient, CPU, GPU, LCD, Mem Controller etc. It works with Yosemite.
So I downloaded this neat desktop app and it showed my fan set on "AUTO" control but the current RPM was ONLY 1200, with the minimum RPM listed as 1200 and maximum as 2640.
The Ambient temp was 8 C (46 F), and the CPU, GPU etc. were at 44 or 45 C (~113 F). So I used the Macs Fan Control interface to set the fan at a constant value of 1920 RPM (right in the middle of the 1200 - 2640 RPM range).
Immediately I could hear a very quiet "whirringgggg" sound of a fan (which I had NEVER heard since buying the iMAC) and can actually feel air coming from the back vent. After an hour or so, the entire back of the IMAC that used to be very warm, now feels "normal" and cool to the touch, not overheated like before.
Now, just an hour after increasing the fan speed from the minimum of 1200 RPM to 1920 RPM, the CPU, GPU, etc. sensor temps are now showing as 37 or 38 C (~98 F). 15 degrees F cooler than before when the fan was only running at 1200 RPM.
I really think this solved the problem of my IMAC shutting off by itself. Maybe there is a problem with the "Ambient sensor" since it is 68 F in the room, NOT 8 C / 46 F. This may have kept the fan running at only 1200 RPM (minimum) while set to the "AUTO" setting.