How is the direction of the hydraulic fluid changed so that the cylinders and pistons can move backwards and forwards?
The direction of travel is controlled by a directional control valve for a double acting cylinder.



The red line is high pressure from hydraulic pump. when the high pressure fluid is entering the right chamber and push the actuator piston to left.
The low pressure fluid in left chamber (blue line) will leave the actuator and return to reservoir.

If we rotated/move the directional control valve clockwise it will look like the figure below :



The high pressure will enter the left chamber and move the actuator piston to right