Reinstalling OSX on a Macintosh is sometimes necessary, whether you are upgrading or whether you need to repair a problem.
The problem is that after Snow Leopard, Apple abandoned DVD media and gone exclusively to downloadable installers. And, many recent Macintosh machines no longer have DVDs, or you may have sacrificed your DVD drive for a second hard drive in your machine. The easiest solution is to have a bootable USB stick with the OSX installation you need on it. Note: It is a good idea to get this made BEFORE you have problems, because you need a fully functioning Mac with an Internet connection to set it up.
First, you will need to log onto the Apple App Store, and download the latest OSX installer. Just do the download, don’t start the installer. Currently, the only choice you have for free is OSX 10.13 High Sierra.
However if you previously had downloaded OSX 10.7, 10.8 10.9, 10.10, 10.11 or 10.12 , you should be able to re-download the installer from your Apple App Store Purchases list. Again just do the download, don’t start the installer. Get yourself a 16 GB USB stick (an 8 GB will do, but I like to be on the safe side – also note that the stick is going to get erased in the process, so don’t use one that has any important data on it.)
Then get the shareware program Diskmaker X which has scripts that will do the rest for you. It will ask you where you have stored your Installer file, and then burrow into it to find the disk image, and Restore that to your USB stick. Diskmaker X is currently having a problem with 10.13, we are waiting for an announcement on a fix.
Some of the Apple installers have a bug in them – if they were downloaded before February 2016, they go out to check an Apple server for authentication, but those servers no longer exist. The fix, as long as you can boot the Mac still, is to set the date back to January 2016 or earlier, then proceed with the install, then reset the date to current.
As an alternative you can find the files manually: http://coolestguidesontheplanet.com/how-to-make-a-bootable-osx-10-8-mountain-lion-disc-or-drive-from-the-downloaded-mountain-lion-app/
If your machine came with 1o.7 or higher, and you haven’t purchased any upgrades, then you don’t have a way to download anything except 10.13. However your machine originally came with a hidden Recovery partition and if you still have the original hard drive (or if you migrated the Recovery partition properly to a new drive) you can still build an installer using Apple’ OSX Recovery Disk Assistant https://support.apple.com/kb/DL1433?viewlocale=en_US&locale=en_US.
One note, never try to install an OSX version earlier than the one that shipped originally with the machine.