Nodes and wallets are given checkpoints like "Don't accept the blockchain unless the block 5000 has hash A, 2000 has hash B, 500 has hash C". And yes, if they find a longer chain then they'll erase their chain and switch! But I don't think they find the longest chain before downloading it, because any node can lie at the beginning.