Thanks for the pics!
They run a machine called a tamper over the tracks, which uses lasers, high-precision differential gps, and magic to align them. As it moves along, stopping at every tie, it picks up the tracks and shifts them to exactly where they need to be, while jamming vibrating chisel-like tools down the side of each rail and in between the ties. The vibrations cause the ballast rocks to jump around and tetris themselves into the correct positions to lock the ties in place. The machine then lowers and lets go of the track, re-measures it, then either tweaks it again if necessary, or scooches ahead to do the next tie. (There may be larger machines that do a few ties worth at a time, I've edited this description to match the machine in the video I linked below.)
Here's some excellent footage of a tamper working away on Waterloo Region's ION light rail tracks a few years back. This section looks a bit weird because it's gauntlet track (two track sections overlapped about 8 inches off center from each other), but the principle is the same for all track: