In my experience Macs tend to spontaneously shut down and re-boot for one of three
reasons.
1) There is too much current draw on the electrical circuit your Mac
is attached to. For example, a refrigerator cycling on, or a laser
printer starting up, will cause a huge draw. It might be enough to
cause your Mac to have a hard crash and then re-boot.
2) Your Mac is overheating. With old Macs this might be due to the
accumulation of dust inside the case insulating the heat coming from
the motherboard. Or detritus in the case might be strangling your
fans. Or your fans may be failing. If it is one of the first two,
opening up your Mac and cleaning it out may be all that is necessary
to fix things.
3) A failing power supply (flyback) or simply cold solder joints
around your power supply. Often the heat cycling in your Mac expands
and contracts the solder joints and causes them to eventually crack
(you can feel these cracks with your fingernail - but don't try this
without discharging your power supply first!) and causes a
discontinuity. Touching the solder joints for the power supply with
the tip of a soldering pen to re-flow the solder easily fixes cold
solder joints (but once again, don't try this
without discharging your power supply first!).