Lexical match in a search engine retrieval

Have you ever wondered how search engine works? How can it match a query with millions of documents super fast and return them to the users?