Make sure to boot into Recovery Mode using Command + Option + R as that will hopefully bypass any authentication with an AppleID so an AppleID is not tied to this Mac which will cause problems for the new owner.
The error you receive may be due to an expired certificate, so you may need to set the date to some time in 2017, or perhaps even 2016, or 2015, etc. Launch the Terminal app from the Utilities menu and enter the following command to set the date & time to Jan. 2, 2017 @ 11:33am (adjust the year accordingly if 2017 does not work to allow the install):
date -u 0102113317
If macOS 10.12.6+ was installed at some point on this Mac, then booting using Command + Option + R will instead access the online macOS 10.13 High Sierra installer (in theory since sometimes Internet Recovery Mode will only access the online installer for the OS which originally shipped with the Mac from the factory).
If you attempt to create a bootable macOS USB installer as suggested by @WheelieNick, then you will need access to another Qualifying Mac if this iMac is not able to boot into macOS or Safe Mode. A Qualifying Mac is one which is compatible with the OS you are trying to download and create a USB installer for. You can use this article to see which versions of macOS are compatible with various Apple hardware to identify a Qualifying Mac.
https://eshop.macsales.com/guides/Mac_OS_X_Compatibility
A Qualifying Mac for creating a bootable macOS 10.13 USB installer is usually a Mac from 2011 to 2018.
A Qualifying Mac for 10.11 is usually one from 2008 to 2015.
See the macOS compatibility article to confirm an exact model will work.