I had this problem with a friend computer, it was an old iMac and I'll tell you it is not going to be easy.
The first thing you have to do is make sure what model you have (the year when your computer has been released) then check on the official apple website to see what is the latest macOS or Mac OS X version available for you computer.
In most of the new mac computer, you can just press cmd+r while booting and the mac will automatically download everything you need to install the system, but the oldest does not have this tool.
In this case, you have to download the dmg file, that can be found on the web, for example, one websites that provides some macOS and Mac OS X is this (for El Capitan, if you need another version, I'm sorry but you have to search for it).
Here things start to get a little tricky.
First of all you have to flash the image on a USB drive, I recommend etcher, that works on everything (Mac, Linux and Windows too) it's extremely easy to use and you just need to select the drive and the image and etcher will do everything by itself, plus it's free here.
When the USB drive is ready you can plug it in you mac and press alt (option) while booting, you have inserted a firmware password, it will ask you to unlock the firmware by inserting that password, else it will take you to all the bootable drives, including your USB device.
If you see the mac logo with a stop icon over it, it means that you downloaded a too new version that is not supported from your mac, else it will start.
When it start, it won't install, saying that the system can't verify the downloaded image, that's why you have to navigate on the "utilities" menu on the top bar and open the terminal.
Now you have to choices, change the date & time, which can work, but may not.
That's basically because every image of mac has a certificate that can expire, so, if the certificate is expired you won't be able to make it work, unless you change the date (the date is different from mac version to mac version, so based on that you have to change it, usually just search for when was that version released and se the current date to that date or even one or two days later to make it work). Then try to install the system, if this does not work again, you can start the installation without verifying the image, but you should really trust the image you're using from being corrupted or modified (just to make sure the download went right, use the SHA-1 code to make the file has been downloaded right).
So, to proceed without verifying the image, from terminal, type in this command:
sudo defaults write com.apple.frameworks.diskimages skip-verify true
then, start the installation. (Is possible that you won't need the sudo at the begin, in that case just remove it from the command and start from "defaults")
Now you should be able to install macOS from a USB drive...
Just a little thing, make sure to have formatted the disk before proceeding, I would recommend to make a partition that takes the whole disk in mac Journaled format, then if you want you will be able to encrypt the disk (the installer will ask you to do that later), instead, if the disk was encrypted before, you will have to insert the encryption key of the disk to continue the installation process.
Really hope this help, I spent a lot of hours to do this on a really old iMac from 2008... and now it works!