There are a lot of different ways this can play out, and the details matter. I will be answering in the context of faa-regulations.
One point before I go further is that visual separation is a thing, and a very common thing in the usa at least. This can be tower-applied visual separation, where the controller has the aircraft in sight and issues instructions to ensure they don't conflict, or pilot-applied visual separation, where one pilot has the other in sight and maneuvers as necessary to avoid a conflict. In either case separation is allowed to reduce below the "standard" separation requirements.
In situations where visual separation cannot be used, the general rule is that aircraft need to be three miles apart. But this is allowed to reduce to only two miles if one is departing and the other is arriving, provided separation will increase to three miles within one minute after departure. This two-mile minimum must exist at the time the aircraft on the runway begins its takeoff roll.
Even in situations where visual separation can be used there are still minimum distance rules between an arrival and a departure using the same runway—these minimums can't be replaced by visual separation. Legal separation can be as low as 1.0, 0.75, or even 0.5 miles depending on the weight and engine configuration of the aircraft involved (using weight and configuration as a proxy for speed).
My point with the above is that your presumption
plane A will at some point overtake plane B, making the horizontal separation between them zero
does not really hold true in the majority of situations. Very occasionally a controller will make a bad blunder and it can happen, as shown in this recent video. In a case like that it's a lot hairier and the controller should be on top of the situation issuing traffic calls and vectors to pry the airplanes apart. As can be seen, if the controller is slow to react an onboard system called TCAS will alert the pilots to the impending collision and will issue instructions which override ATC instructions. All airliners and many larger business jets have TCAS these days.
But most of the time the controller will act much sooner—the video above is really the exception which proves the rule. Standard separation has quite a lot of buffer built in; if the controller does suffer a loss of separation there is still plenty of time to rectify the situation and prevent a collision. Even if it takes a few seconds for them to react it's almost never the case that the aircraft going around will catch up to the aircraft departing, provided the two aircraft are of similar performance classes.
As for actual methods to prevent a collision:
- The classic is to instruct the go-around to "offset left/right of the runway." This isn't a standard instruction found in the controller rulebook but it is a very common one "in the field," especially at towers which work a lot of aircraft in the VFR pattern; of course this is a bad idea if the weather is bad and the pilots can't see any obstructions near them. This instruction all but guarantees the aircraft won't hit, provided the go-around offsets to the opposite side relative to where the departure is turning.
- If an offset isn't possible the controller can issue turns to avoid conflict; the go-around turns away from the departure and vice-versa. Again this can less effective in bad visibility because pilots will need to gain altitude before being comfortable turning away from the obstacle-free area along the runway centerline. (There are published missed approach procedures but in most cases ATC will issue a vector instead of allowing the published missed, which likely won't provide the immediate separation they need and often conflicts with other streams of aircraft too.)
- The go-around can be issued a climb to above the altitude the departure is climbing to, and/or the departure can be stopped at a lower altitude than they were issued initially. Most of the time they'll need to keep climbing very soon in order to avoid obstacles or terrain on the ground, but a short level-off is better than being run over by the go-around...