I assume that the probe is travelling in a straight line? I also assume that each planet is busily orbiting the sun. How on earth (pardon the pun) did they work out the exact speed the probe should travel at (I also assume it accelerates) in order to fly past each planet and moon at exactly the right point of time and space?
The short version is - for the most part these trajectories can be calculated with a decent degree of accuracy with relatively simple equations. Rocket engines tend to give acceleration in short bursts (which simplifies the computations a lot as well), so New Horizons was launched out of Earth orbit at about 16km/s and since then has only fired its own engines for short correction burns.
Simple ballistics. The probe is still subject to gravity and it can receive trajectory adjustments as needed. It's not like it was launched ten years ago and then it was on its own: it's been piloted remotely from the Earth as necessary.
It's a remarkable achievement but the science behind it has been known and used successfully for decades.