No harm will probably be done, especially if the engine is still running. If the engine is not running, some damage could be caused by thermal shock. So, pouring cold water on a very hot “switched off” engine is not recommended (unless the engine is on fire!).
I can’t think of any other circumstance where externally dousing a hot engine with cold water could be necessary or helpful, but your question does trigger another overheating issue that is very common and where the correct procedure for adding water (to the radiator, not the engine itself) is important.
If ever your temperature gauge rises noticeably above normal (even well short of the red line) you should promptly assess and if needs be physically check why that is happening. Excessive heat is exponentially damaging and the sooner you slow down or stop the engine and remedy the cause the better. It is preventing the engine from getting any hotter, not how fast you cool it down, that matters.
If you spot the problem before the needle reaches the red line, stop the car (but let the engine keep idling). The probability is that no damage has been done…yet. If the temperature needle starts to return towards normal, leave the engine idling for a few minutes before turning it off (at idle the amount of heat being generated is much reduced, but the built-in cooling system is still working, even if not perfectly, and the fan should still be drawing air over the system even while the car is standing still).
The world doesn’t end if you switch off too promptly, but the temperature needle will probably keep rising even though the engine has stopped. That is because you have also turned off the cooling system, and residual heat is now radiating from the hottest parts of the engine (hundreds of degrees higher than the radiator which your temperature gauge measures) as part of its cooling process.
If the needle is already on the red line before you stop, and especially if there are also pungent smells or unusual noises, some damage may already have been done and the sooner you turn off the engine the better. You will have plenty of time to ponder what to do next.
Engines overheat because the temperature they are generating is more than their cooling systems can cope with – either because the engine is generating excessive heat (not enough oil? Ignition timing excessively advanced?) or the cooling system is defective (not enough coolant, the water pump has failed, fan belt has broken, the thermostat has jammed shut, cooling jacket channels are blocked, radiator hoses have split, radiator fins are clogged with mud?).
Whatever the diagnosis of cause might be, the great probability is that by the time the problem has brought you to a halt the radiator will need to be refilled with coolant. This is where we get back to the “pouring cold water” bit.
If there is plenty of water at hand, by all means, pour some over the top tank of the radiator (not the engine) before you open the radiator cap. If you open the cap of a radiator that has been at red-line temperature, you won’t find gently simmering water waiting to poach an egg – you will be met by a rocket blast of scalding steam, strong enough and hot enough to peel a human being. The same might happen if you try to add cold water to a still-hot and empty radiator too quickly without the engine running.
Clearly, some cooling time (and/or helpful cold water) is necessary before you open the cap, which at any stage should be done at arm’s length, with gloves or a heavy towel cloth, in two stages (there’s a halfway position which lets pressure out but keeps the lid on), and with fast reflexes at the ready.
That’s kept you safe. Now keep the car safe, and turn the engine on again – at idle – as you pour cold water or other coolant into the radiator. The engine must be running as the coolant level is restored.