Few waves break straight on to a beach. Most break at an angle. As the swash comes in, it pushes sand with it also at an angle up the beach.
When the force of the swash is spent, the water runs back directly down the beach because this is the steepest angle. So the water does not flow back in the same direction as it came in.
This zig-zag movement of sand is called longshore drift it carries sand sideways across the beach.