Most likely the hard drive is failing. You can try running the Apple Diagnostics to see if any hardware issues are detected. Unfortunately the diagnostics do not detect most drive failures.
The simplest option would be to install macOS to an external USB3 SSD so you can boot from the external USB3 SSD which may allow you to continue using this iMac. However, some hard drive failures may affect the performance of the computer even when booting to an external drive because macOS will try to scan & mount the failing internal drive (some times it may be possible to configure macOS to ignore the internal drive, but it may not always work for extreme drive failures).
I hope you already have a good backup, because if I'm correct that the internal hard drive is failing, then you won't be able to easily retrieve data from that drive...in fact every attempt to read or fix it will make the drive failure worse where even a professional data recovery service won't be able to retrieve any data.