I am sure that you have ever close tabs in Safari with CMD + W and you have not "braked" in time, so the last tab has been closed and therefore also the Safari window. Personally, it bothers me a lot, and you may not like this behavior either, but the positive part is that it has a solution.
Very easy
To do what safari do not close with the last tab you have to follow the following steps:
- Open System Preferences and go to Keyboard
- Go to Keyboard shortcuts and select Applications
- Create a new function for the Safari application with the name «Close tab» and assign it the shortcut CMD + W
Clever. With this trick Once done, you can check that if you go to Safari and leave CMD + W pressed when you reach the last tab, the window will remain intact and will not close. The explanation is very simple, and it is that OS X assigns the shortcut only for that command and removes it from all the others, which makes the default behavior to be disabled and we can close the tabs at full speed without fear of closing window.
Source - Barrettj
More information - How to change autofill options in Safari browser