The "gravity assist" flyby technique can add or subtract momentum to increase or decrease the energy of a spacecraft's orbit. Generally it has been used in solar orbit, to increase a spacecraft's velocity and propel it outward in the solar system, much farther away from the Sun than its launch vehicle would have been capable of doing. Since a flyby can also decrease a spacecraft's orbital momentum, the Galileo spacecraft decreased its energy, relative to Jupiter, with a gravity assist flyby in front of the Jovian moon Io. In this way, it was possible to decrease the mass of rocket propellant needed for Jupiter orbit insertion. Comets and other bodies in solar orbit naturally experience changes in their orbits once in a while, as they happen to pass close by a planet or a moon.
Slingshot — Not!
Individuals have often compared gravity assist with the effect of a slingshot. In terms of physics, though, gravity assist is a different example. Consider using a slingshot: a person would sling a projectile around a few times, stronger and better aimed each time, before letting it go. Upon letting go, the projectile's centrifugal force becomes its propulsive force. On the other hand, using a gravity assist, a spacecraft comes up and steals some angular momentum during a single flyby of a planet in motion, removing momentum from that planet. Gravity assist is really much more like a baseball connecting with a fast-moving bat than it is like a slingshot.