Back up all data to at least two different storage devices, if you haven't already done so. The backups can be made with Time Machine or with a mirroring tool such as Carbon Copy Cloner. Preferably both. You must be certain that you can restore everything to the state it's in now.
Boot into Recovery (command-R at startup), and reinstall OS X. Test. If the problem is gone, you're done.
Note: You need an always-on Ethernet or Wi-Fi connection to the Internet to use Recovery. It won’t work with USB or PPPoE modems, or with networks that require any kind of authentication other than a WPA or WPA2 Personal password.
If the problem persists, boot into Recovery again, launch Disk Utility, and erase the startup volume with the default options. This operation will destroy all data on the volume, so you had be better be sure of your backups. Quit Disk Utility and install the OS again. When you reboot, you'll be prompted to go through the initial setup process. That’s when you import the data from one of your backups. For details of how this works, see here:
Using Setup Assistant
Import only "Users" and "Settings" – not "Applications" or "Other files." Don't import the Guest account, if it was enabled on the old system. Test. If the problem is still there, you have a hardware fault. Take the machine to an Apple Store for diagnosis.
If the problem is resolved, reinstall your third-party software cautiously. Self-contained applications that install into the Applications folder by drag-and-drop or download from the App Store are safe. Anything that comes packaged as an installer or that prompts for an administrator password is suspect, and you must test thoroughly after reinstalling each such item to make sure you haven't restored the problem.