If you think of it in terms of conservation of momentum and collisions, the simplest version works just the same as tossing a handball at a on-coming freight train. The interaction is elastic, and the ball returns with the same speed it had going in in the center of momentum frame, but the center of momentum frame is moving in the ground frame, so the ball is going faster in the ground frame.
And it is that matter of there being multiple frames that you didn't pay enough attention to. The incoming spacecraft leaves the planet (or moon) with the same speed that it had coming in, but only in the center of momentum frame for the interaction. And because the planet (or moon) is moving in the frame of the solar system (i.e. the sun's frame) the spacecraft can be going faster in the solar system coordinates.
As CuriousOne noted in the comments, you can also lose energy in the solar-system frame. That's equivalent to throwing the ball at the back of the trai as it moves away from you.
Some basic math. We'll assume that our freight train is only moving at $+10 \,\mathrm{m/s}$ and that we can throw the ball at $20 \,\mathrm{m/s} with respect to the ground. Also that the collision is elastic and straight on.
So, you stand in front of the on-coming train and hurl the ball at it at $-20 \,\mathrm{m/s}$ in the ground frame. The center of momentum frame is essentially the trains frame, so in that frame the ball approaches the train at $-30 \,\mathrm{m/s}$. It hits and rebounds at $+30 \,\mathrm{m/s}$ in the train frame which is $+40 \,\mathrm{m/s}$ in the ground frame.
You'd better duck the ball even before dodging the train.
The details of the spacecraft-planet interaction are more complex in the sense that there will be motion across the line of motion instead of just in out, but they both represent nice elastic collisions, so the math remains very similar in spirit.
Note that I have not addressed the fancier version of the maneuver in which the space-craft fires its engines near periapsis which allows the maximally efficient use of fuel, but you should start understanding this basic effect.