Good question.
See there are many sites on the internet. But when people search for some particular keyword then they usually use search engines to perform their searches. This search engines normally shows 10 results per page and most of the users click on the top 4 to 5 results.
The sites which are listed on that top 4 to 5 results are the sites getting most traffic from the search engines for free.
This can lead to more revenue and more sales for the website owner.
So he or she optimizes the site to get better positions for related keywords for their sites in the search engines.
Now how it works is a whole new thing.
It depends on the search engine algos by which they define the position of the web site in their index.
Most common things are the backlinks and content along with proper keyword selection.
Keyword selection:
You search for the high search volume keyword with less competition and target that keywords.
Backlinks:
You exchange links with other webmasters having relevant sites to yours or you make one way links by doing the directory submissions, article submissions, social bookmaking, forum posting, blog posting and link baiting.
Content:
You write or get it written some original content for your site which is related to your site and which can be crawled by the search engines.