To stop the editor from playing, from the forums, thanks to Romz89:
UnityEditor.EditorApplication.isPlaying = false;
And, as Eric said, OnApplicationQuit () can capture the application shutdown process as you ask, but you could also use OnDestroy () in a singleton for instance.
I’m pretty sure Application.Quit() also works on mobile and probably on video games.
Finally, I can’t add to Ashkan’s answer, so I’ll just point he got a nice solution for quitting on the WebPlayer which will probably not trigger any kind of event, so be sure to manually trigger them. Getting events from a closing web browser is probably very hard even today, but there might be some way: javascript - Event onBrowserClose for Google Chrome Extension? - Stack Overflow
for web pages you can use javascript and document.close () to close the web page but the browser will ask the user about it so it's not forced. you can use Application.OpenURL to change the web page to another one so user can not play your game anymore.
It's not possible to quit in the context of the editor...a game is "run" and "quit" when you're in play mode or not, respectively, which can't be overridden by code. Quitting isn't something that exists in the context of web pages, either--the user chooses to close the page or not; you don't have web content that closes a page for you.