My MacBook never had this problem until I replaced the magsafe adapter with an non-Apple charger (the Ostrich Replacement Macbook Charger 60w Magsafe L-Tip Power Adapter Charger) during the heat of summer. Whenever I was working the CPU heavily while the ambient air temperature was hot (20-25C or so), first the monitor would repeatedly shut off, and soon the entire machine would spontaneously shut down completely. The computer felt quite hot, so at first my solution was to shut it down for 20 or more minutes to let it cool off. But whenever I used the MacBook for long periods of time in hot temperatures, even this proved to be only a temporary solution. Ultimately, I ended resting the machine on top of a medical cold pack just to keep it running for an hour or less at a time. I then tried three of the solutions mentioned in this thread.
First, I tried resetting the PRAM, but this had no effect at all, as far as I could tell.
Second, I followed @jurgenkoppen's suggestion and downloaded smcFanControl, but unfortunately it didn't help nearly enough. I then switched to a different fan controller, MacsFanControl (https://www.crystalidea.com/macs-fan-control/download), which worked much better. It's slightly more complicated to use than smcFanControl, but not by much; I set it to display the temperature of the CPU Diode, which consistently ran very hot very quickly, and I set the exhaust fan to run on auto. These steps immediately began to cool down the whole computer, and maintained a relatively lower temperature.
Third, after reading that a faulty power adapter could be responsible, and realizing that I had just recently replaced mine, I tried removing the power adapter and running off the battery. The result was striking: whenever the power adapter was in use, the temperature would climb VERY high fairly quickly, but when the battery was in use, the temp would drop to significantly cooler levels.
My MacBook still runs too hot when I work it hard, probably because of faulty hardware, but it doesn't shut down anymore after I made these changes.