The most obvious suspect is the PRAM battery.
Depending on precisely which G5 iMac you have:
How to replace the PRAM battery:
You could also try a SMU reset:
iMac G5: How to Reset the SMU
Started having issues with my 2005 G5 iMac, not the same as yours, but when told to sleep would shutdown and then fans take off...while you have the back off replacing the battery per Klaus1's recommendations, look at the small capacitors towards the lower right side (looking at the backside), they look like tiny beer cans with an X on the top...see if any are bulging or showing a pale compound on the surface. There were problems with the capacitors in the 2005 G5 iMac...mine has about half a dozen that are bad so fitured that was the big issue with the iMac.
If that is the case you may want to think seriously about a new iMac as they cannot be repaired for a reasonable cost.