Whenever you search for something on Google, the search engine goes through more than 30 trillion web pages, and, in just half a second, it finds the top 10 results that answer your question. How is this possible? Google doesn't actually visit every page on the Internet each time you ask a question, but it stores information about web pages in its databases, and uses algorithms that read those databases to decide what to show you.
These databases are built through programs called spiders, which continuously scan the web’s pages. Google is constantly adding new pages to its database, or updating pages as they change. When you perform a search, Google takes your query - the text you typed in the search bar - and scans its database to find the most relevant web pages.
To do this, Google uses an algorithm called PageRank, which Google founders Larry Page and Sergey Brin created for their PhD thesis in 1998. Page and Brin noted that a webpage's importance can be estimated by looking at what other important pages link to it. Thus, through algebraic calculations, PageRank assigns each web page a score based on the PageRank scores of every other page that refers to it, scores which depend on the pages that link to these pages, and so on.
Once Google has found all the pages in its index that mention your search query, it then ranks them using PageRank and other criteria, such as the location from which the query was made, or how recently a web page has been updated, and ignores websites that appear to be spam. However, remember that Google's search algorithm is constantly evolving, with minor updates being launched over 500 times a year.