Google crawls the web and indexes the pages it finds. When you type in a search query, the ranking algorithm selects web pages from the index that might be a good match for that search, and ranks them using hundreds of factors. The exact formula is a trade secret, and the Google engineers are constantly adjusting the formula to produce better results. What Google representatives have confirmed is that they look at the content on each web page, this is the most important factor. The other two major factors are the link profile of a site, which websites are linking to it, and machine learning, which they call RankBrain. Most SEOs agree there are several dozen other factors which have a sizeable influence on overall rankings.