Why does the moon not leave its orbit around the earth due to the gravitational force from the sun?
The speed of the Earth (and Moon) around the Sun is $\approx 29.8 \frac{km}{s}$.
The speed of the Moon around the Earth is $\approx 1.05 \frac{km}{s}$.


The result is that the Moon has a nearly perfect circular orbit around the Sun, the effect of the Earth is only a perturbation in it:



In fact, the "waves" are much smaller (400.000km Moon orbital radius to  150.000.000km Earth orbital radius)

This is mainly because $R_{Earth-Moon} \ll R_{Earth-Sun}$, and $M_{Moon} \ll M_{Earth} \ll M_{Sun}$. In general, 3-body gravitationally bound systems tend to be unstable, i.e. one of the components tends to fly away with time. But this system seems stable, because the ratio of the masses are big.

The same can't be said from the whole Solar System, (it is a notoriously unsolved problem) although also this seems stable in human time scales.