The origin of this idiom . . . like many . . . is debatable. However, the most widely accepted explanation is based in aviation. On airplanes, the throttle control handles . . . or joystick . . . and the fuel mixture are often topped with grips that are ball shaped. When a pilot pushes the ball into the full thrust position . . . towards the firewall . . . it pushes the aircraft to go as fast as possible.
On a side-note, if you've ever wondered why a joy stick is called thusly . . . well, chances are that is indeedy a vulgar term . . . the obvious implication that a (male) pilot was gripping the stick between his legs; his joystick.
Another explanation involves steam engines. These engines have a mechanical regulator that utilize of a pair of hinged lever arms with a ball on the end of each arm. As the engine speeds up, centrifugal force causes the arms to rise up closing a valve. The regulator can be adjusted so that the arms go to horizontal (with the balls pointing to the wall) without closing the valve. This adjustment does not limit the speed of the engine so that it can go full force.
A similar explanation refers the automatic speed control for a diesel-generator . . . like those used on submarines. Inside the hydraulic governor round counterweights are attached to a vertical drive shaft. The weights (balls) are on hinged arms. As the engine spins, the drive shaft spins and slings the balls outward toward the walls of the governor housing. The faster the engine turns, the closer the balls get to the wall, i.e., engine at high speed, balls to the wall.