Newton very much relied on the works of his predecessors, and sometimes his contemporaries. This is what scientists do do, and should do. Newton did not "figure out" his universal law of gravitation completely on his own. He instead expanded upon the works of Galileo, Kepler, Halley, Huygens, Hooke, and others.
There are issues with regard to priority for Newton's law of gravitation. Hooke, in particular, claimed that he discovered this prior to Newton. Apparently Newton and Hooke independently discovered the inverse square relationship. In Hooke's favor, Newton switched from a centrifugal point of view in his first version of his Principia to a centripetal point of view as espoused by Hooke in the second version of Newton's Principia. What Hooke did not do was to connect that inverse square relationship to a force proportional to the product of the objects' masses, or connect that to how things fall toward the Earth, or to how the Moon orbits the Earth.
Most science historians give credit to Newton. It was Newton who put together that complete package. This complete package said that it was a force that made objects fall toward Earth, made planets orbit the Sun, and made the Moon orbit the Earth, and that this force was proportional to the product of the objects' masses and inversely proportional to the square of the distance between them. This force was universal.
Newton's predecessors and contemporaries were beginning to question the Aristotelian physics that remained the dominant world view in Newton's time. However, those other scientists had not quite divorced their thinking from the Aristotelian view that the physics of what happens on Earth and the physics that happens in the heavens were very distinct things. This unification of earthly and heavenly physics was perhaps Newton's greatest contribution to science.
Galileo had shown that different objects fall toward the Earth with the same acceleration. Whatever force caused this falling must therefore be proportional to the object's mass. By Newton's third law, that force must act on the Earth in an equal but opposite manner. By symmetric, that force should be proportional to the product of the Earth's mass and the object's mass.
Kepler had shown that planets orbit the Sun along elliptical paths, with the square of the period proportional to the cube of the distance between the planet and the Sun. Newton derived that this means an inverse square relationship in the special case of circular orbits early on in his works. Showing that this is also true for Kepler's elliptical orbits took a great deal more effort. That gravitation does follow an inverse square law is Newton's Proposition LXXIV (proposition 74) in his Principia. A large number of propositions and analyses precede this key proposition.
It was an apple and the Moon that provided Newton with the connection between Galileo's studies and Kepler's laws. Some say the story of Newton's apple is apocryphal. The cartoon version of an apple bonking Newton on the head is just that. However, a number of Newton's contemporaries gave credence to the basis of the story. William Stukeley claimed to have been there first hand, when he and Newton were taking a walk through a garden. An apple fell toward the ground, just not on Newton's head.
Newton was struggling at the time with explaining the Moon's orbit. This turns out to be non-trivial. As a coarse measure, the Moon's period is consistent with the acceleration of a falling apple. However, the Moon's orbit has a number of so-called inequalities, some of which were known to the ancients. An elliptical orbit caused by the gravitational force between the Earth and Moon explains the largest of these inequalities. Others, such as evection (known to Ptolemy) and variation (discovered by Brahe) can only be explained by the fact that both the Earth and Moon gravitate toward the Sun.
It took Newton twenty years after his walk with Stukeley to arrive at a reasonably accurate explanation of the Moon's orbit. Newton's writings on this topic changed significantly between the first and third versions of his Principia. By the time he was finished, his lunar theory agreed to within a few arc minutes of detailed observations of the Moon made by Halley.