Consider momentum first. Any spacecraft-planet
interaction (takeoff, landing, crash, flyby) conserves momentum, when the
planet's velocity-change is equal to that of the spacecraft multiplied by
(spacecraft mass)/(planet mass).
This mass ratio involves typically well over 20 powers of ten, and therefore to 20 or more figures, the planet's velocity is unchanged.
So the spacecraft's speed may change by several km/sec while a typical planetary speed-change is an atom-width per year. No wonder that Jupiter is unaffected by a flyby!
The gravitational interaction conserves energy, too. This means that the passing spacecraft's distant-approach and distant-departure kinetic energies (and hence speeds) are the same relative to the planet.
That is, relative to the planet the craft's distant-velocity vectors have the same length -- gravity simply rotates their directions.
In the frame where the planet is moving steadily, this is an acceleration.
At upper right the diagram shows the path of a spacecraft aimed behind a
planet moving with effectively constant velocity V.
The spacecraft's outgoing speed |vf| is greater than its incoming speed |vi|.
This is demonstrated below to the left, in the addition triangles for the velocity vectors. The spacecraft's distant velocities relative to the planet (depicted by dashed lines) are the same length but rotated as arrowed.
Evidently the gravitational pull aligns the spacecraft's motion more closely with that of the planet and it is swept along, gaining speed.
To brake on arrival, NASA may use the slingshot effect by aiming the craft in front of its target planet.
The slingshot effect is not an isolated oddity of classical mechanics.
Identical physics operates when you throw a light elastic tennis ball at 10km/hr towards the front of a heavy truck which is oncoming at 80km/hr. The truck's progress is unaffected (momentum, mass ratio) and the relative speed of 90km/hr is reversed (energy). The ball gets a terrific boost and comes back to you at 170km/hr.
If dodging trucks is too dangerous, then go into the lab and try the double-ball drop.
These examples involve repulsive contact, not a gravitational attraction, but the conservation principles and large mass-ratio feature just the same.
Think too about heavy bats, rackets, clubs, etc striking light elastic balls. And indeed next time you inflate a bicycle tyre and the pump gets hot in your hand, think of each tiny air molecule picking up speed (kinetic energy) as it bounces elastically from the advancing piston.