An undercut is pitting before the car you are chasing, using fresh tyres to set faster laps while the rival is still on old ones, so you emerge ahead when they stop. The overcut is the opposite: staying out longer to build a gap while the rival warms up cold tyres.
The undercut works because a new set of tyres is at its fastest almost immediately, while a worn set is at its slowest. Two or three quick laps while the car ahead is struggling can be worth more than the time lost in the pit lane, and the pass happens on the timing screens rather than on track.
The overcut works when the opposite is true. On circuits or compounds where new tyres take a lap or two to reach temperature, the car that stays out is quick while the car that stopped is slow, and the gap grows in the other direction.
Which one applies changes with the track, the compound and the air temperature, and teams work it out in practice rather than assuming. It is also why a driver stuck behind a slower car will report the gap to the pit wall constantly: the decision to stop is being made a lap at a time.