There are two possibilities. The first is the system is just going into sleep mode. A quick hit to the keyboard, trackpad, mouse or power button should wake the system (not restart).
If the system restarts then you likely hit a crash event. Make sure your apps are up to date for your version of macOS.
You may need to review the crash files [https://www.cnet.com/tech/computing/tutorial-an-introduction-to-reading-mac-os-x-crash-reports/|Tutorial: An introduction to reading Mac OS X crash reports]
You might want to run the onboard diagnostics as well. Restart your system and press the ***D*** key to enter
Reference: [https://support.apple.com/en-us/HT201255|Mac startup key combinations]