It's time to talk of how the Google search algorithms work but I don't want to bore you so better visit the mentioned page. Just keep in mind that these algorithms analyze hundreds of different factors to try to surface the best information the web can offer, from the freshness of the content, to the number of times your search terms appear and whether the page has a good user experience. In order to assess trustworthiness and authority on its subject matter, they look for sites that many users seem to value for similar queries. If other prominent websites on the subject link to the page, that's a good sign the information is high quality.
There are many spammy sites on the web that try to game their way to the top of search results through techniques like repeating keywords over and over or buying links that pass PageRank. These sites provide a very poor user experience and may even harm or mislead Google's users. So they write algorithms to identify spam and remove sites that violate Google's webmaster guidelines from the results.