Inside one chain, the rule is easy: a coin spent in an earlier block cannot be spent again later, and a block containing a conflict is invalid. The hard case is when two valid chains exist at the same time.
Most work, not most blocks
Every node follows the valid chain that contains the most accumulated proof of work. It is often called the longest chain, but the exact rule is the heaviest: a chain of fewer blocks mined at a higher difficulty can outweigh a longer one. In practice the two almost always agree.
Valid comes first. A chain with more work but an invalid block, a forged signature or an oversized reward, is ignored by every node, whatever it cost to mine. Work only decides between chains that already follow the rules.
▶ SEE IT IN THE MACHINEThe BLOCKCHAIN rail shows the chain every node agrees on: the one with the most work behind it.