Basically, hypothetically and conceptually:
ranking = importance x relevance +/- quality signals
Importance
Search engines try to rank the more important sites first:
*importance is largely based on link popularity (Google calls it PR) - the more links, especially from higher PR sites, the better --> seen as more important a site --> rank higher
* links from authority/expert sites (ie Hilltop theory) may also be an 'importance' factor
* lots of good content makes sites want to link to you as you are a good resource
Relevance
Search engines try to rank the most relevant sites higher. To be more relevant to a particular keyword(s) you need:
* Keyword(s) in the title, description, H1 tags and have the highest density on the page, but still keep low as possible and flow natural with the text
* Keywords in the anchor text of incoming links - this is currently the most important relevance factor
* links from other sites on the same theme/topic (ie TopicSensitive Page Rank theory)
* good content (so you get the relevant links)
Quality signals
Search engine try to rank the better quality sites first, so look for "signals":
eg
* do nothing that gives any hints that you are participating in link exchanges for the purposes of ranking better (ie link for the benefit of the user)
* don't link to a viagra or casino site unless you are a viagra or casino site
* duplicate content issues
* etc