If have a recent Mac, you can use OS X Recovery to reinstall OS X.
These steps reinstall the latest version of OS X that you previously had installed on your Mac. If you want to reinstall the version of OS X that came with your Mac, hold Command-Option-R at startup instead. This starts your Mac from Internet Recovery.
Restore from a Time Machine backup
If you have a backup of your system that you created with Time Machine, you can Restore from a Time Machine Backup to reinstall OS X and your personal files. Selecting this option erases the drive you're restoring to and restores the version of OS X that was installed at the time you created the selected backup.
Erase your drive and install OS X
Generally you don't need to erase your startup disk to reinstall OS X. The OS X Installer is designed to allow you to perform an install in place over the same version or earlier versions of OS X. If you're transferring your Mac to a new owner, you might want to erase your built-in startup disk before reinstalling OS X.
Important: Before you erase and reinstall, back up your important files. If you’re using a MacBook, MacBook Air, or MacBook Pro, make sure the power adapter is connected and plugged in while performing these steps.
You can use these steps to erase the files on your startup disk and install a new copy of OS X:
If you want to reinstall the version of OS X that came with your Mac, or if you need to reformat your entire startup disk, hold down Command-Option-R at startup instead. This starts your Mac from Internet Recovery.
If you have more than one partition (like Boot Camp) and you want to erase the entire startup disk, you can use Disk Utility to remove partitions or repartition your drive. Start your Mac from Internet Recovery, then use Disk Utility to repartition your startup disk before you erase it. Make sure you back up any important data from all partitions before repartitioning a drive.