A DC motor's direction depends on which terminal is positive. Connect + and - one way, it spins clockwise. Swap them, it spins anti-clockwise. To reverse it under software control, you'd need to physically swap the wires — which is what relays are for, but four of them is excessive. The elegant answer is the H-bridge.
Four switches, two diagonals
Draw four switches around the motor in an "H" shape: two on top (one to each side of the motor's + V supply), two on the bottom (one to each side of ground). Closing the upper-left and lower-right switches simultaneously puts +V on the left motor terminal and ground on the right — the motor spins one way. Closing upper-right and lower-left reverses the polarity — the motor spins the other way.
In silicon, the four switches are usually MOSFETs or BJTs in a single chip. The L298N and DRV8833 are common Arduino-compatible H-bridge ICs. You drive each switch with a digital pin; the chip's internal logic prevents the "shoot-through" failure mode where both switches on one side close at once and short the supply to ground.
PWM into an H-bridge gives you speed AND direction
Combining direction control with PWM gives you a controller for any DC motor: pick the direction (which diagonal is on), modulate one of the on-switches with PWM (which sets the average voltage), and you have full forward and reverse speed control. This single circuit drives every cordless drill's reverse switch, every robot vacuum's wheels, and every drone's brushed-motor variant.