How can I close apps properly/shutdown the system via ssh?
I have to start by saying that there is nothing wrong with using halt or shutdown from the command line.

In fact if you issue either of those commands then any running applications will get exactly the same message from the system as if you chose "Shut Down..." in the Finder.

The advantage of using sudo shutdown is that you can shutdown the system regardless of which users are logged in.