All blocks have a field for their 'difficulty'. This is a parameter that determines how many hashes have to be calculated (on average) before you find a block. Higher difficulty means the block will be harder to solve.
How can a block be considered more difficult to solve/hash than another one?
Think of it like flipping coins, where the difficulty number is the number of Heads you have to flip in a row. So, With difficulty 8, you have to get 8 heads in a row, etc. Difficulty 9 is harder than a difficulty 8.
This difficulty number is used to increase/decrease the number of blocks solved on the network, according to very specific rules, in order to keep blocks solved at an average of 10 minutes. If the network is solving too many blocks, say a block every 9 minutes on average, then the network will increase the difficulty. Similarly, if the network is only solving blocks every 11 minutes at the current difficulty, then the difficulty is lowered. This re-adjustment process happens every two weeks in Bitcoin.
The tricky part is what can happen if a fork takes place at the same time as the difficulty adjustment. Let's say our blockchain looks like this:
X_8
/
-> X_8 -> X_8
\
X_8
(Where 8
is the difficulty of each block). Since the top fork and the bottom fork have different time fields in their block header, they result in a different difficulty re-adjustment calculation (difficulty re-adjustment is based on number of blocks in a given time, trying to keep it at a block per 10 minutes). Now the next blocks on either side of the chain, which have the newly calculated difficulties, at not given the same weight.
X_8 -> X_9
/
-> X_8 -> X_8
\
X_8 -> X_7
Since the difficulty re-adjustment in the top fork ended up with a higher difficulty, any block solved on that half of the chain (X_9) will override a block solved on the bottom side of the fork (X_7). However, if X_7 had been solved significantly before the X_9 block, then that would be the chain with the most work (8 + 7 on the bottom chain is better than just 8 on the top chain).
The network essentially looks for the chain of blocks with the highest sum of difficulties, i.e. the chain that took the most work to produce, and would be the hardest for an attacker to try to override. Nodes on the network choose this highest-difficulty-sum chain as their correct chain, and the great thing about this is that when everyone follows the simple rules, they arrive at consensus about the history of transactions.
Does it have to do with the number of transactions it contains?
No.