They follow a set of rules, with the main rules involving the location and frequency of keywords on a web page. Search engines operate the same way. Pages with keywords appearing in the title are assumed to be more relevant than others to the topic. Search engines will also check to see if the keywords appear near the top of a web page, such as in the headline or in the first few paragraphs of text. They assume that any page relevant to the topic will mention those words right from the beginning.
Frequency is the other major factor in how search engines determine relevancy.
A search engine will analyze how often keywords appear in relation to other words in a web page. Those with a higher frequency are often deemed more relevant than other web pages. All the major search engines follow it to some degree, in the same way cooks may follow a standard chili recipe. But cooks like to add their own secret ingredients. Nobody does it exactly the same, which is one reason why the same search on different search engines produces different results.