OS X Safari prompt before exiting
I remap Safari’s Quit menu item shortcut to ⌥⌘Q (Option-Command-Q) using the “Keyboard Shortcuts” tab of the “Keyboard & Mouse” preference panel in System Preferences.
As Scott noted in a comment, modern macOS versions have a checkbox labeled "Close windows when quitting an app" in the "General" section of System Preferences:
Unchecking that box will cause closed applications to remember their open windows and their content, and to reopen them when you restart the application.
Alternatively, if you prefer to enable this feature only for Safari, you can select "Safari opens with: All windows from last session" from the "General" tab in Safari preferences:
Note that this Safari-specific option is hidden when the general "Close windows when quitting an app" System Preferences option is unchecked, since it would be redundant in that case.
Also, as noted by Chris Johnsen, you can rebind the "Quit Safari" menu item to a different, less easily triggered keyboard shortcut (e.g. ⌥⌘Q) via System Preferences → Keyboard → Shortcuts → App Shortcuts:
Unfortunately, AFAIK there's no easy way to rebind the same shortcut for all apps, since the name of the menu item is obviously different for each app.
All screenshots taken and described behavior tested on macOS High Sierra version 10.13.4 with Safari 11.1. Obviously, later versions may change how things work.
Safari will prompt if you've edited a text field but not submitted the form yet.
Otherwise, you can get back to where you were after an accidental Quit by going to History > Reopen All Windows From Last Session.