Computer overheating power off issue

Hi,
My computer shuts down unexpectedly, while i am in the middle of working. It just power offs.
I turned it back on and after beeping ,it displayed the following message: CPU was shutdown due to a thermal event.
I then have the option to proceed with starting up the computer normally or to enter set-up of the system.
A few times it failed to start back up and the only way i was able to get it back on was to remove the power cable from the power supply and plugging it back in.
I think that my computer is overheating and i tried installing a CPU cooler, but the one that i purchased did not fit.
What exactly do i need to do to prevent my computer from overheating?
I noticed a few days ago that if i leave the computer open ,the frequency decreases.
Thanks.
