It's a fascinating story really. Newton was trying to understand what kept the celestial bodies in their orbits. He understood the concept of centripetal force, and realized that if the gravity of the Earth extended out to the moon, then the gravitational pull of the Earth on the moon could be the centripetal force that keeps the moon in its orbit. He then calculated the necessary pull and found that it was about 1/3600 of the pull that the Earth exerts at its surface. Since the moon is about 60 Earth radii away, he hypothesized that if the force fell off as the inverse square, then everything would work out. This wasn't too hard to believe, since it was known that things like the intensity of light fall off as the inverse square, because the area over which the light is spread increases as the square of the radius. Deducing that the same pull of gravity which causes things to fall to the Earth could also explain why the moon doesn't fall to the Earth was, in my opinion, a tremendous stroke of insight and a testimony to Newton's genius. It's worth reading Newton's own words on this topic:
“In the beginning of the year 1665 I found the Method of approximating series & the Rule for reducing any dignity of any Binomial into such a series. The same year in May I found the method of Tangents of Gregory & Slusius, & in November had the direct method of fluxions & the next year in January had the Theory of Colours & in May following I had entrance into ye inverse method of fluxions. And the same year I began to think of gravity extending to ye orb of the Moon & (having found out how to estimate the force with wch [a] globe revolving within a sphere presses the surface of the sphere) from Kepler's rule of the periodic times of the Planets being in sesquialterate proportion of their distances from the center of their Orbs, I deduced that the forces wch keep the Planets in their Orbs must [be] reciprocally as the squares of their distances from the centers about wch they revolve: & thereby compared the force requisite to keep the Moon in her Orb with the force of gravity at the surface of the earth, & found them answer pretty nearly. All this was in the two plague years of 1665-1666. For in those days I was in the prime of my age for invention & minded Mathematicks & Philosophy more then than at any time since.”