How to download and install macOS - Apple Support
Yosemite is old enough that it may not be able to access this forum, that Support page, or the App Store. You may need to upgrade your iMac to El Capitan or Sierra first – and then, from the old Mac, use the High Sierra link in the Support article to get the rest of the way to High Sierra.
E.g., download the .DMG file containing the Sierra installer to a USB flash drive, transfer that drive to the old iMac, copy the .DMG file to the old iMac, then open it and run the installer within. Or copy a bookmark to the location of the Sierra .DMG file to a USB flash drive, transfer the bookmark file to the old iMac, use the old iMac to download the Sierra .DMG, and proceed from there. (The Sierra file is behind a plain http link, not a https one – so even the version of Safari in Yosemite may be able to download the file if given the plain http address.)
Before you start, it would be a good idea to make a bootable clone backup of your current system. Do not rely on Internet Recovery as your only way to recover if something goes wrong.