For the 9mm/.40/.357 full-size pistols, a close look at the frame grip will tell you the generation:
For Gen4 and later, they stamp the Generation (Gen4, Gen5) on the left side of the slide. Gen5 models have a similar gripping pattern to the gen4, but without any fingergrooves.
Gen1 and Gen2, mechanism is virtually the same. Starting with Gen3, they added the weapons-light rail on the dustcover portion of the frame, but most parts would still interchange with Gen1 and Gen2 (with a few exceptions, such as locking blocks). Some Gen3 pistols will have 2 frame pins like the Gen1 and Gen2 (one at rear of grip, one over trigger); other, later models will have three (adding the second smaller pin over the trigger, called the locking block pin). Starting with Gen4, many more internal changes, including new-style dual-spring recoil spring assembly, reversible magazine catch for left-handers, and modular changeable backstraps to add size (and beavertails) to the basic grip. Much of the internal mechanism was the same, but small tweaks required slightly different parts. Gen5 is darn near a whole new animal, many major internal changes, different barrel rifling, ambidextrous slide stop lever, different springs in several areas, back to 2 frame pins, back to no fingergrooves.
This is not an all-encompassing list, but it hits most of the high points. Larger frame/caliber Glocks did NOT get the same changes at the same time, nor did the subcompact 9mms/40s (see below), so don't apply this photo or list to other frame sizes/calibers.
.