If yours is an iMac 21.5-inch Mid 2011, the inside bits can be viewed in this
iFixit repair guide (logic board view, shows all internals; see section 33.)
{in this view, the optical drive and cooling fan assemblies are both removed}
https://www.ifixit.com/Guide/iMac+Intel+21.5-Inch+EMC+2428+Logic+Board+Replaceme nt/5959
{There are no parts in the upper right behind the display that would account
for the noise; however upper center, is where the hard drive is located. And
a rotational hard drive can make clicking noises; before they die. And when
they do go out, usually the symptom and results are much like you say.
•See the main section of that specific guide to see the steps to get into the
location where the hard drive lives... Note: IF 2nd HDD was installed, it may
have been placed in upper right; an option, a small 2.5" could be fitted there.}
(You may choose to seek expert help; see steps in main guide to access.)
If yours is an iMac 27-inch Mid 2011, detailed guide or takeapart image for
a model identified as such by MacTracker.ca database - is not in iFixit guide.
Seems to look alot like 21.5 inch, except small details; they share some IDs.
The graphics processor and CPUs usually are different between 21.5/27-in.
So the suggestion is to consider an appointment at Apple store Genius to see
if they can determine a more exact cause of failure; it may be too old for them
to actually repair it. They may offer to send it out for repair to a certified shop.
-Or you could consult an Apple Authorized Service Provider; where they could
do a diagnostic a&give you a repair estimate, variable based on their findings.
The hard drive is likely candidate for replacement with new one; the AASP
can be sure to install a correct one that has a matching Temperature Sensor
so the iMac won't run fans constantly; other parts not quite right, may be a
cause of a noted annoying problem where the cooling fans default to high
and do not shut off. Apple part number replacement hard drive has sensor.
{A backup from your computer, if one exists, could be migrated to your
iMac once repaired; backups on external storage drives & those a Time
Machine utility could make, help restore a Mac. Otherwise the failed HDD
is a vortex for lost data, a costly venture if data recovery service is hired.}
In any event...
Good luck & happy computing! 🙂