You need either a spare empty USB Hard Drive or USB Flash Drive over 8Gb in size.
Reformat it in Disk Utility:
Select the drive to reformat > Erase > Give it a Name > Format: Mac OS Extended (Journaled) > Scheme GUID Partition Map
Then whilst you are connected to the internet:
Open Terminal > paste in the Sierra script (at the prompt) from the following link > hit return and let it do its stuff.
How to create a bootable installer for macOS - Apple Support
It will take a while whilst it downloads and installs the Installer System on your external drive.
When it is installed, plug your drive into the iMac and restart holding down the option key and selecting the external Drive (you gave it a name).
How to select a different startup disk - Apple Support
Hopefully you will see the iMac mounted and the hard drive on it. If not we'll take it another step.
Peter