There are a couple of ways to look at this. One is to note that the Moon does orbit the Sun. (If you plot the heliocentric path of the Moon it follows a wavy path that moves silghty in and out from the Sun, but is always curved towards the Sun.

So why isn't the Moon pulled away from the Earth by the Sun? Because both the Earth and Moon orbit the Sun together. The force the Sun exerts in pulling the Moon away is not due to the force the Sun directly exerts on the Moon, but due to the difference between the acceleration of the moon due to the Sun's gravity and the acceleration of the Earth due to the Sun's gravity. And this difference is smaller than the influence the Earth's gravity has on the Moon.