Command-W not working correctly on Safari ⌘W always worked in my Safari like this: 


it closes all tabs
if there's no tab, it closes the window.


Now, #2 is not working anymore.

Any clues?
Go to your System Preferences… application, and select the Keyboard pane.

Hit the Keyboard Shortcuts tab on top, and Application Shortcuts on the left. Is there an entry for Close Tab ⌘W or an entry for Close Window that's set to something else? If so, remove it with the +- widgets at the bottom of the window.

Without knowing what other applications you're running, that's the first place I can think of to check what might be intercepting it.