Most big generators are 'synchronous' machines.
They consist of a static magnet on the rotor (in principle, in practice these are regulated electromagnets - but the point is that they are fixed to the rotor and rotate with it). The stator then connects to the grid. This device will act as both motor and generator. The rotor magnet will align itself with the rotating magnetic field in the stator when motoring, and the voltage in the stator will align itself with the stator field.
As a motor, the machine will remain in sync with the grid - that they can and have been used for clocks (in the days before radio receivers were integrated into clocks, you could get atomic stability by using a synchronous motor to drive the clock mechanism).
The problem with synchronous motors is that they have a 'let go' torque. Gradually increase the load, and the motor will start to lag a few degrees behind the grid. However, at a certain torque (usually occuring at around 10 degrees), the motor will experience a sudden drop in torque and stall.
Exactly the same thing happens with generators. If you drive them too hard, they experience a very rapidly increasing braking torque, and gradually edge a few degrees ahead of the grid (this has the side effect of generating a large amount of 'reactive' power). Similarly, if you don't drive them hard enough, they start to lag behind the grid, and they will act as motors (consuming energy and reactive power).
This behavior of sync. machines ensures that they tend to stay connected - but the control systems for the rotor exciter and the generator drive just need to make sure that the generator is generating the appropriate amount of power, and is within appropriate limits for frequency.
There are problems in certain conditions, which may cause loss of sync. Imagine a diesel generator in a small town, at the end of a long power line to the larger grid. Normally, it will operate synchronized. However, what happens if there is a brief short-circuit at the town's substation (maybe a squirrel jumps onto a transformer). Because it is close to the generator, the generator will supply the bulk of the short circuit current, with only a modest amount coming via the long, high-resistance power lines. This places a huge braking force on the generator causing it to slow down. The diesel engine may have a delay in response - first the slowing of the generator rotor must be detected, then the fuel pump instructed to give a higher fuel dose, and then the fuel must be injected. Remember a diesel cylinder only fires once every 2 revs. So a 4 cyl engine at 1800 rpm, only fires once per 60 Hz cycle. This means, it's going to be a minimum of 16 ms, before the engine can can deliver increased power - and in practice it's going to be 50 ms. However, under short circuit conditions, the generator can decelerate very fast (e.g. 10 rpm/ms), and in 50 ms it could fall 20 degrees behind the grid, and like a sync. motor that's overloaded, the rotor will simply 'let go' and synchronization will be lost. Only option if that happens is to disconnect from the grid, resync and then reconnect.
The same thing can happen with sudden loss of load - generator picks up speed because the engine can't respond quick enough, leading to the rotor 'letting go'.
The ease of 'letting go' is partly determined by strength of the rotor's magnetic field. At maximum strength, there is good resistance to loss of sync - but at low field strengths (e.g. when generating at partial power) then it is very easy to lose sync. This is a particular problem when you have 2 power plants connected together - if partial power is required by the grid, it's possible for one plant's generator control to drive it hard, allowing the other plant to coast at very low power. Any kind of grid disturbance could then knock the low power plant off line due to loss of sync. Real generator control systems will have systems that prevent this type of coasting by specifying minimum power levels.