When you turn the key, you close an electrical switch which energizes the solenoid. This does two things. First, it starts spinning the starter motor, which is just a DC electrical motor. Second, it pushes out the starter motor shaft so that the gear engages the flywheel. The flywheel just happens to be a convenient place to grab a hold of the engine’s moving parts. There’s no law that says you have to turn the flywheel, and in fact (IIRC) the early hand crank on cars turned the main crankshaft. The real main purpose of the flywheel is just to be a big hunk of metal. It’s large mass means that it doesn’t want to change its motion very quickly, so it helps to smooth out the engine’s jerkiness.
There is also another electrical switch which is turned on when you turn the key. This one turns on electricity to the ignition coil. The ignition coil is just a coil of wire. When you energize it, the changing amount of electricity (going from off to on) causes a large magnetic field to develop. As the engine spins, it also spins a thing called the distributer. All the distributer does is connect the coil to the various spark plugs as it spins around. In other words, as the distributer spins, it’s going to momentarily connect the coil to spark plug 1, then it will spin a little bit further and connect to spark plug 2, etc. When it connects, the energy in the coil is discharged through the spark plug, making a spark. This ignites any gas in the cylinder.
Mechanically, the engine will open valves to let gas into the cylinder, then close the valves as the piston starts to move up. Once the gas/air mixture is compressed, the spark will cause it to burn very quickly. This miniature explosion forces the piston down and turns the shaft. Then another valve opens and as the momentum of the engine pushes the piston back up, it pushes the exhaust gasses out. Then the gas valves open back up, and as the piston goes back down (still from the momentum of the engine) it sucks gas into the cylinder, and the whole thing starts all over again.
The tricky part to making it all work is the timing. There is a thing called the timing chain, which rotates a cam shaft above the engine. This shaft opens and closes the valves at just the right time, so that gas goes into the cylinders and the exhaust comes out of the cylinders, and also that all the valves are closed when the cylinder fires so that all the energy goes into pushing the pistons down.
Once you get the engine going, the momentum of the engine will keep the pistons moving up and down, so at that point it will run forever on its own and doesn’t need any help. You then let go of the key and the starter motor is disengaged (the little arm with the gear retracts away from the fly wheel) and the starter motor stops spinning. The second electrical contact to the ignition coil is still connected, which makes the sparks as the engine rotates. When you turn off the key, all you do is remove power from the ignition coil. Without spark, the gasoline doesn’t explode, and the engine stops fairly quickly.
Real cars these days aren’t quite so simple. There are things like electronic ignition and fuel injection and such, but that’s the basic idea of an internal combustion engine. Your lawn mower, weed wacker, etc. all work on basically the same idea.