Search engines look at the content of a web page to identify the topic on which the web page talks. It then identifies the quality of the content by looking at different factors of the web page such as length of the content, how many times a keyword is appearing in the content, apart from text does the web page uses images / sound / videos to explain the content etc. Such factors which can be optimized by a webmaster are call "On-Page Optimization"
Because on-page optimization can be manipulated by webmasters easily search engines ranks websites based on the popularity of a webpage on internet. Search engines do this to filter out the spammers from the search results and serve most relevant results to the users for their search. Search engines checks how popular a web page is on other websites in the same category of topic, how popular the website is in social media, blogs, news etc. Increasing the popularity of the website on the internet is known as "Off-page Optimization".