Is it only the block height or is it the effort that went into the forks? In other words: does this decision account for different difficulties?
Gary's answer is not entirely correct. When comparing two chains, their total "scores" are compared. Each block counts as (2^256 / block_target); this is the expected/average number of attempts that were necessary to create it.
Obviously, within one series of 2016 blocks, the difficulties are all equal, so for most small reorganizations, the score will tell you the same thing as just counting the number of blocks. However, as an attacker may well try to rewrite blocks that cross the multiple-of-2016-boundary, you need to take this into account. The actual rule always favors the chains which required the most "work".
According to the Satoshi paper, the following applies:
The steps to run the network are as follows:
1) New transactions are broadcast to all nodes.
2) Each node collects new transactions into a block.
3) Each node works on finding a difficult proof-of-work for its block.
4) When a node finds a proof-of-work, it broadcasts the block to all nodes.
5) Nodes accept the block only if all transactions in it are valid and not already spent.
6) Nodes express their acceptance of the block by working on creating the next block in the chain, using the hash of the accepted block as the previous hash.
Nodes always consider the longest chain to be the correct one and will keep working on extending it. If two nodes broadcast different versions of the next block simultaneously, some nodes may receive one or the other first. In that case, they work on the first one they received, but save the other branch in case it becomes longer. The tie will be broken when the next proofof- work is found and one branch becomes longer; the nodes that were working on the other branch will then switch to the longer one.
That pretty much sums it up. If you have more valid blocks, you win until someone comes along with more valid blocks.