Short answer: macOS has no switch to disable Cmd W, but you can move it, per app, in System Settings.
The steps are below. If you are closing whole apps rather than tabs, that is Cmd Q, and there is a separate guide.
Cmd W closes the front tab. That is nearly always what you want, which is exactly why it is dangerous: you fire it without looking, dozens of times a day, and the one time the tab was a half-written form or a search result you spent ten minutes finding, it is gone with no confirmation.
It also sits directly beside Cmd Q. Miss by one key and you do not lose a tab, you lose the entire window.
In Safari, Cmd W closes the tab and Shift-Cmd-W closes the window. In apps with no tabs, Preview or TextEdit, Cmd W closes the window outright, which is why the same keystroke feels inconsistent.
macOS lets you reassign any menu item's shortcut. Point Cmd W at something harmless and it stops closing anything.
Close Tab.Cmd W now does nothing in that app, and the menu shows the new combination.
The menu title has to match exactly. macOS matches on the literal text, so Close Tab and Close are different strings, and a mismatch does nothing at all with no error to tell you why. Open the app's File menu and copy what you see.
Before changing anything, learn the undo. Shift-Cmd-T reopens the last closed tab in Safari, Chrome, Firefox and Edge, and works repeatedly to walk back through several.
Safari also has History, then Reopen Last Closed Tab, and Reopen All Windows From Last Session after a full quit. Text you had typed into a form is usually gone regardless, which is the part people actually mind.
App Shortcuts works one app at a time. There is no global switch, so protecting Safari, Chrome, Mail and your editor means four entries, and a new machine means doing it again.
It is also all or nothing. Once moved, the shortcut is gone in that app, including the times you did mean to use it, and Cmd W is genuinely useful most of the time.
The behaviour most people actually want is not a disabled key, it is a question. Keep Cmd W working, and have it check before closing something in the handful of apps where losing a tab hurts.
That needs something between the keystroke and the app, which is what Still Dock does. You choose the apps, it asks first in those, and Cmd W behaves normally everywhere else. It guards Cmd Q the same way, which matters given the two keys are neighbours.
Using more than one screen? The Dock also jumps between them, which is a separate problem with its own fixes: how to keep the Mac Dock on one screen.
Not in System Settings. App Shortcuts is per application by design, so a global change needs something that sits between the keystroke and the app.
No. Closing still works, from the menu or from your new shortcut. You have moved the shortcut, not removed the command.
Because those apps have no tabs, so the front thing to close is the window. In tabbed apps the window shortcut is usually Shift-Cmd-W.
Cmd Q quits the whole app. See how to disable Cmd Q, which has the same App Shortcuts trick aimed at Quit.
It brings back the page. Text typed into a form is usually lost, which is why not closing it in the first place is worth more than any recovery trick.