Short answer: macOS has no switch for this, but you can reassign Quit to a shortcut you will never hit by accident, in System Settings.
The steps are below, along with why Q of all keys, and what to do about the tabs you already lost. Everything here applies to Cmd W as well, which is the key you were actually aiming for.
Look at where the keys are. Q sits directly above A and immediately left of W. Three of the most-used shortcuts on the Mac live on that cluster:
One is a rounding error away from the other two, and only one of them is catastrophic. Miss W by a few millimetres and Safari does not close a tab, it closes everything: every window, every tab, and any form you had half filled in.
Nothing warns you. There is no undo. That is the whole problem, and it is a design decision Apple has never revisited.
You cannot switch Cmd Q off globally, but you can move it, per application, so the dangerous shortcut is no longer under your little finger.
Quit Safari.Cmd Q now does nothing in that app. The menu updates to show the new combination.
Two things that trip people up. The menu title has to match character for character, including the app name, so it is "Quit Safari" and not "Quit". And you have to repeat this for every app you want protected, which is the main weakness of the approach.
Worth doing regardless, because it turns a disaster into an inconvenience.
In Safari, open Settings, then General, and set Safari opens with to All windows from last session. Quit by accident and reopening puts every window and tab back.
It does not recover text you had typed into a form, so it is a safety net rather than a fix.
Both already ship the guard that Safari lacks.
That these browsers all added it independently tells you how common the complaint is. Safari is the outlier.
The other approach is to make Command Q require intent, by adding a short delay so a tap does nothing and only a deliberate hold quits. That covers every app at once instead of one at a time.
Several tools do this, including free ones. It is the right shape of solution: you keep the shortcut you already know, it just stops firing on a graze.
Still Dock, which locks the Mac Dock, is adding quit protection as a switch in the menu bar, alongside the Dock locks, so the same app that stops your Dock changing by accident also stops your apps closing by accident. Same idea, applied to the other thing macOS lets you destroy in one keystroke.
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. The App Shortcuts panel works per application, so a global change needs a utility that sits between the keystroke and the app.
No. Quit still works, from the menu or from your new shortcut. You have moved the shortcut, not removed the command.
macOS matches App Shortcuts against the literal text of the menu item. "Quit Safari" and "Quit" are different strings, and a mismatch simply does nothing, with no error to tell you why.
In apps that use the standard document model, yes, you get a save prompt. Browsers and many others just close, which is why the browser case hurts most.
Reopen the app and check its History menu, and in Safari use History then Reopen All Windows From Last Session. Text typed into a form is usually gone.