Something they don't teach you about driving a car is that doing so is all about ignoring little temptations. When you're in control of a massive metal machine, that little kid in the back of your head wants to do whatever silly little thing it thinks up, from revving the engine in park to mindlessly mashing all of the buttons on the dashboard. Obviously, you shouldn't do any of that stuff, as it's all bad for your car and potentially hazardous to your health. Even so, it's normal to wonder what, exactly, would happen.
For example, what would happen if you were to turn your car key to the off position or even remove it entirely while the car is in motion? Or, in the case of wireless key fobs, what would happen to the car if the key went out the window mid-drive? Unlikely and ill-advised as they may be, automotive manufacturers have actually taken these scenarios into consideration.
If, hypothetically, you were to turn your car's ignition cylinder to the off position and remove the key, then your car's engine would immediately shut off. Of course, thanks to the laws of physics and whatnot, all that kinetic energy from your car's ongoing movement wouldn't just immediately vanish and stop you dead.
When the engine is turned off, it stops taking fuel from the intake, but any fuel already in there keeps the pistons pumping for a bit longer, pushing the car forward until the pistons stop. Additionally, your car's engine or battery doesn't power your steering wheel and brakes, so they would still maintain at least partial control. Basically, if you turn the car off while driving, it'll stop being a car and start being a wagon.
Incidentally, if your car has a wireless fob that, through some random happenstance, leaves the car's confines, it wouldn't actually affect anything. Most cars that utilize this kind of system have failsafes built-in that keep the car running normally even if the fob goes too far away or if the stop button is pressed. Once you turn the car off, you would need the fob to turn it back on, but that's a separate concern.
What you should do
If, for whatever reason, your car suddenly shuts off completely while you're driving, either due to a mechanical failure or you spontaneously deciding to turn the ignition off, you can still move yourself out of danger.
If you're on the road, you should shift into neutral so the wheels aren't dependent on the engine. After that, you can use the steering wheel and brakes to carefully guide the car's leftover movement to the side of the road, where you can come to a complete stop. In the event your brakes lock up, you can still slow and stop yourself by using your car's emergency brake.
This particular setup exists for a reason: in the event your car suffers a dangerous mechanical failure or even catches fire, you need to be able to turn off the engine and steer yourself to safety. Obviously, you should never instigate this yourself outside of such emergency situations, but the bottom line is that if you manually shut the car down, it'll just slow to a gradual stop.