How can I close apps properly/shutdown the system via ssh? I need to know if there's a proper way to close apps via ssh and if it's posible shut down the system without the sudo halt or sudo init 0 (because I think those ways are not good for the system when there are apps running)
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.