In most age-group competitions (e.g. masters, high school, etc) any short-course race longer than about 400m will have lap counters for each lane.
(I say "short course" because of the number of lengths involved - for short course meters, SCM, the 400m is 16 lengths of a 25m pool, and for short course yards or SCY, which is widely contested in the USA, the 500y will be 20 lengths of a 25y pool. A big masters SCY meet will have the 500y, 1000y and 1650y events contested. Olympic events are LCM or Long Course Meters, so I'm not sure which events would get counted there; the 400m is only eight lengths or four laps.)
The lap counters (N.B. this links to a sporting-goods site selling them) are pretty low-tech: they're waterproof boards with a handle at the top and numbers that can be flipped to indicate the number of lengths completed. A counter is positioned at the opposite end of the swimmer's lane from the starting blocks and will dip the board in at the wall as the swimmer approaches to show them their progress. Then they yank the board out before the swimmer turns so the board isn't kicked during the turn. Here's a short video showing lap counters at an age group meet, probably counting a 1650y race.
In my experience this means the board (a) always shows an odd number, counting 1, 3, 5, etc., (b) usually is turned from one number to the next when the swimmer turns at the block end of the pool, so when two laps are completed the counter is turned to 3, etc., and (c) for the last length the counter will show two orange squares indicating the swimmer should finish after the next length.
The counters handling the boards are usually volunteers, often teammates of the swimmers. I've counted for my brother dozens of times. When the counter is known to the swimmer there's a chance of communicating additional data, e.g. if the swimmer is ahead or behind their goal pace, by swinging the board from side to side or pumping it up and down. Sometimes the swimmer will ask not to be shown a count for every lap in order to keep their head clearer.
Ideally the officials at the block end of the pool will be monitoring the counters to ensure they're not asleep on the job, but miscounts sometimes happen as they do in track races.
Pro/international level races might have a more sophisticated system (and in LCM probably fewer races will have enough lengths to merit counters), but this is what I've seen in place for the majority of meets I've watched or competed in.