How does a gravity slingshot actually work?
Here's an intuitive understanding without math or physics explanations (others will provide that stuff here):

You are right that approaching and leaving the vicinity of a planet in itself adds up to zero effect. Gravity assist is the effect of being "dragged along" with the movement of the planet. If a spaceship approaches the planet from behind in its orbit, it will be dragged along and accelerated. If a spaceship approaches from infront of the planet in its orbit, the spaceship will slow down as the moving gravity field of the meeting planet pulls it backwards.