How do Trains not Collide?

Let’s start with this: based on

latest data rail transport is extremely safe.

Between 2010 and 2018, an average of 71 Americans died in train related accidents compared to 36.560 auto deaths in 2018 alone.
As safe as the railroad transportation is, air travel is still the safest.

How do trains not collide?

There are single and double track railways.
A double-track railway usually means trains running on one track in each direction, simple and easy.
A single-track railway is a railway where trains traveling in both directions share the same track.
However this doesn’t answer the main question.
Pretty simple:

planning is everything.

The use of tracks is scheduled by trains, just like airlines have a schedule for all its planes, for specific takeoff or landing slots.
The controllers keep track of the trains with the help of signaling systems, so if one looks like it is going to be someplace where it shouldn’t be, the controller radios the locomotive engineer to take action to avoid disaster.
It is also possible that two trains have to use the same part of rail at the same time. This will happen only if there is a sideline available. One of the trains will switch to the sideline, to get out of the way, while the other passes. If there are no sidelines available on the track, they schedule the trains so they don’t use that part of the track at the same time.
Railroads have control stations as well. Their role is to monitor, switch the signals and tracks to make sure that no trains are using the same piece of track.

Positive Train Control

(PTC) is the newest method, system, designed to prevent train-to-train collisions, over-speed derailments etc. It uses sensors to determine the trains location along with GPS, direction and speed of trains, and can automatically apply the brakes on a train that is in danger of collision with another train or other track conditions that could cause a train derailment or crash.
PTC will not prevent accidents caused as a result of track or equipment failure such as improper vehicle crossing through a grade crossing, trespassing on railroad tracks or certain types of train operator error.
When Positive Train control is not available, the engineer and conductor have to be watchful to follow the rules, instructions, signal indications, and remain alert to avoid train collisions.