The Apollo missions used a technique called a free-return trajectory, which was designed to ensure that if something went wrong during the mission, the spacecraft would return to Earth safely. This trajectory involves using the gravitational pull of the Moon to slingshot the spacecraft around it, which would cause the spacecraft to follow a curved path or a ‘figure-eight’ shaped path or a circumlunar loop.
The reason for the shape of the outbound (or ‘translunar’) trajectory was to make the spacecraft pass by the ‘leading edge’ of the moon rather than the ‘trailing edge’. The reason for passing the leading edge was to produce a trajectory so that if there was a complete failure of spacecraft propulsion, the moon’s gravity would send the spacecraft on a trajectory back to the earth that would arrive at the earth at (or very near) the proper conditions for re-entry.
If the spacecraft would have passed the trailing edge of the moon, and there was a spacecraft propulsion failure, the moon’s gravity would have sent the spacecraft on a trajectory that was not possible to recover it from.
The reason for the shape of the inbound (or ‘transearth’) trajectory had to do with the thermal environment that the spacecraft heat shield would be subjected to on re-entry.
This is because, approaching east-to-west (opposite to the earth’s rotation) would have increased the spacecraft's velocity relative to the atmosphere, and resulted in higher heat loads on the spacecraft.
This is a great 6 minute clip that explained it to me, with visuals of the trajectory and gravitational pull of both the Earth and the Moon.