To answer the original question (which hasn't quite been done yet), is that the sun is not pulling the earth, or the moon, in "towards" itself in the way you are thinking. In circular motion, an object tries to move tangentially to a second object. But force of attraction caused to motion to not be purely tangential, which would cause the first object to continue to move to infinity, but rather to be angled slightly towards the second object. This happens at each moment in the motion, giving rise to a circular orbit, rather than the naturally linear one.
The key thing to observe in circular forces is the resultant velocity. The sun's force on Mars is greater than its force on Earth, but this only causes Mars to circle to sun faster and at a smaller radius. Circular forces do not pull object towards the center of the orbit, they only change the way in which the objects circle around the center.
So it wrong to ask why the sun does not pull the moon away from the earth, because the sun's force is only making the moon rotate around the sun, not to be drawn towards it.