There is a fair chance the hard disk drive &/or the cable, may be at fault now.
The hard disk drive is old, probably worn, and likely is the cause or major
contributor to the symptoms. There also is a clock battery in the logic board.
These kinds of items could be addressed more easily in your old vintage iMac
G5 or early Intel model because unlike newer ones, it is more readily accessible.
The iFixit.com repair guides for Macs are helpful; sometimes general items not
in one guide, are summarily covered to a passable degree in a similar model.
Depending on this actual hardware build model you have, the guides will differ:
• iMac Repair - iFixit (from powerPC G3 G4 G5 -- to recent Intel models)
https://www.ifixit.com/Device/iMac
The G5 was produced until the Intel based iMac (and other intel mac) arrived
in 2006 model year. However new old stock G5 were for sale past that time.
Since those transitional models look a lot alike, the devil is in the details.
The computer is now too old for the Apple Store (retail official) to offer a repair
though diagnostic may be available via the Genius via appointment. You could
then choose to visit an authorized Apple service provider who fixes older gear.
You could probably most carefully attempt to fix it. Notable issues are sensors
on or near the hard disk drive bracket, and other places where if the sensor is
not able to detect the heat or is disabled, the fans may always run fast & loud.
So there are 'details' in those details. Cause and effect... Or after-effects. 🙂