Search Is Mostly Preparation
The illusion that a search engine sweeps through the entire web in the moment you press Enter is exactly that: an illusion. If it actually did, no result would ever come back fast enough to be useful. What really happens is that nearly all the work was completed in advance, before you ever opened the page. The search engine has spent days, weeks, and years building up its own private library of the web, and your query is answered from that library, not from the live internet itself.
Crawlers: Reading the Web on a Schedule
To build that library, search engines run armies of automated programs called crawlers or spiders. Their job is to wander the web by following links from page to page. Whenever a crawler reaches a page, it downloads the content and notes every link it sees, so other crawlers can visit those pages too. By repeating this process constantly, the search engine builds up an enormous map of the public web — a list of pages and what they contain.
The Index: A Giant Sorted Table
The raw pages crawlers collect are not stored as random text. They are processed into a huge, carefully organized database called an index. In broad terms, the index works like the index at the back of a book: for every word, it records which pages contain that word and where. When you search for something, the engine looks up your terms in this index, which lets it find every page on the web that contains them in a fraction of a second — without ever opening those pages live.
Ranking: Picking the Best Few
Finding the matching pages is only half the job. There are usually millions of them. The next step is ranking: deciding which results to show first. Search engines use complex algorithms that consider hundreds of signals, including how closely a pages content matches your query, how often other pages link to it, how recent and trustworthy it appears to be, and many more. The result is the ordered list you finally see.
Crawl, Index, Rank
The whole pipeline really boils down to three big stages: crawl, index, and rank. Crawlers gather pages, an index turns them into a searchable structure, and ranking picks out the best matches when you ask. All of this is why a search engine can answer in under a second something that would otherwise be an impossible task: sifting through billions of pages in the time it takes to blink.
Source
This article was written using information from Wikipedia.