I've done it, decades ago, back in engineering school, on devices in the single digit KW class, using techniques mostly out of the 1930s. The concept is rather simple. You adjust the speed of the machine to be the same as the power line. Then, you adjust the voltage of the machine to exactly match the power line (where exactly can be in the milliVolt range, despite a multi-hundred or multi-thousand volt output from the machine). Then, you adjust the phase of the machine so that it exactly matches the power line. And, at just the right moment (because those conditions of matched frequency, phase, and voltage only exist for an infinitesimally small period of time!), you close the main breaker. If you got everything exactly right, and were lucky, the machine locks to the power line, and all is well. If you were unlucky, the breakers pop, possibly explosively. If you were really unlucky, and the breakers fuse, large multi-ton rapidly spinning machines explosively disassemble themselves. You don't want to be in the same building when that happens.
Now, there are automated, computerized devices that assist in synchronizing generators (well, actually, alternators). Those tend to make the process MUCH easier and simpler, when they work. When there's a bit of noise on the line, or harmonic distortion, if can cause things to mismatch, which produces a fail-to-lock scenario, at least until some frustrated low-level person is forced to push the manual override under pressure from upper management to get the thing back on line. In that case, see the previous paragraph about explosive disassembly.
We won't even get into power factor effects, line delays, and harmonic distortion causing difficulties.
The amazing thing isn't that it's so hard to synchronize alternators; the amazing part is that it's possible at all!