I have heard that the Voyager spacecrafts received gravitational boosts from some of the planets before they left the solar system. How is this achieved? Since we have to conserve energy it seems that whatever speed is gained due to gravity would then be lost as the object flys away from the planet.
Specifically I want to know how we can willingly slow the planets orbit to steal some of its energy. How do we know that the fly by is going to take energy from the planet rather than lose energy to the planet?