How Google Search Works

What happens when you perform a Google search and how does Google decide which pages are shown at the top of search engine rankings? How do ads fit in and how does Google remain impartial?