How does a spacecraft get to where its going?

How can we use planets and moons to "slingshot" a space ship into a different path?

Slingshot paths are called Gravity Assist Trajectories. They use the gravity and motion of planets to pull a spacecraft into a new path.

How does this work? Consider Voyager 2, which toured the planets from Jupiter and beyond. The spacecraft was launched on a standard Hohmann transfer orbit to Jupiter. Had Jupiter not been there at the time of the spacecraft's arrival, the spacecraft would have fallen back toward the Sun, and would have remained in an irregular orbit as long as no other forces acted upon it. Perihelion, or the low point of the orbit would have been near Earth, and aphelion or the high point of the orbit at Jupiter's distance of about 5 AU.

However, the spacecraft's arrival was carefully timed so that it would pass behind Jupiter in its orbit around the Sun. As the spacecraft came into Jupiter's gravitational influence, it fell toward Jupiter, increasing its speed toward maximum at closest approach to Jupiter. Since all masses in the universe attract each other, Jupiter sped up the spacecraft substantially, and the spacecraft slowed down Jupiter in its orbit by a tiny amount, since the spacecraft approached from behind. At this point, Voyager 2 had been sped up enough by Jupiter's gravity to get a speed greater than Jupiter's escape velocity. As it left, it slowed down again, but it never slowed all the way to the speed it was before getting to Jupiter. It left the area near Jupiter faster and in a different trajectory. This technique was repeated at Saturn and Uranus.

Gravity assists can be also used to decelerate a spacecraft, by flying in front of a body in its orbit. When the Galileo spacecraft arrived at Jupiter, passing close in front of Io in its orbit, Galileo experienced deceleration, helping it go into orbit around Jupiter.

Thanks to NASA JPL's Basics of Space Flight for help with this answer.

How can a spaceship leave orbit?
How does a spacecraft change course?
What causes an orbit to happen?
How does gravity work in space?

How can we tell a spacecraft's speed?
What are planets made of?
How does speed affect an orbit?
What is escape velocity?

How do spacecraft use an orbit to move from planet to planet? What is a Hohmann transfer orbit?
Why do mass and distance affect gravity?