If a body of negligible mass (like a space probe) travels without active propulsion in the gravity field of a more heavy object (like a planet or the sun) which is traveling at constant speed (which is a good approximation for the short duration of a swing-by maneuver), then the path of the first body in the reference frame moving with the second is a conic section. This includes hyperbolas and ellipses, with parabolas as limiting case between them and circles as another special case.
As a general rule, the orbit is a hyperbola if the speed of the object is enough to break free of the gravitational pull, as the asymptotes of the hyperbola are lines going to infinity. A body describing an ellipse is gravitationally bound and can't escape unless it gains more energy.
Once you have more than two bodies in your system, or comparable masses, things become a lot more difficult. But at least for the general idea of why a swing-by works, going from one two-body situation with the sun in its center to one with a planet and back to one with the sun works well enough. For actual mission planning, more work would be needed.
In case you do read German, I did write a Facharbeit in my final year at school about the subject of swing-by, deriving the properties mentioned above and many more. Would write it different these days, but it might still be interesting.