The purpose of a flywheel in an engine is to store energy. The energy stored by the flywheel helps steady the rotation of shaft when the torque applied to the shaft is uneven.
A flywheel is a disc with it's weight concentrated toward the outer circumference. When the disc is spun it stores energy by virtue of its angular momentum. This means that it resists changes to its rotational speed. When the load applied to the crankshaft is uneven, as with a piston, the flywheel keeps the crankshaft turning smoothly in between power strokes. In an internal combustion engine the pistons only provide power during one in every four strokes. The flywheel keeps the crankshaft turning smoothly during the other three rotations.
Additionally, the pistons and connecting rods are offset from the crankshaft and want to push the crankshaft from side to side with each piston stroke. The energy stored by the flywheel dampens this process and reduces engine vibration, thus balancing the engine. By maintaining engine speed and balance, flywheels help to extend the useful life of other components connected to the engine. By adjusting the weight of the flywheel, the engine can be tuned to work at optimal efficiency under a variety of work loads.