Using The Terminal Command to Shutdown, Restart and Sleep My Mac?
The command you are after is shutdown.
This  informs all users that the machine is going to be shutdown and tells all apps to close files etc.
The command takes a parameter -h, -r or -s to shut down, restart or sleep the Mac.
The command has to be run as root so you need to use sudo.
e.g. to reboot the machine immediately
sudo shutdown -r now

e.g. to shutdown the machine in 60 minutes
sudo shutdown -h +60

From comments there are two things to be addressed
How shutdown works is by sending a sigterm to all processes which should then deal with that e.g. save open files etc. If they don't exit then they will get sent a SIGKILL which forces them to die with no chance to respond. The signals are not sent via the normal key message queue so Apps have to deal with this separately to the code that gets called from quit on the menu. A good app should call common code from both.
This other answer shows how to shutdown as if you hit the menu options. But note that apps can cancel this shutdown