When 2 blocks are generated at the same time, the agreement on which block is the "winner" depends on the next block.
If there's blocks b1 and b2 for height H, the block that is mined at H+1 determines which of b1 and b2 is on the main chain. If H+1 built on b1, then b1 is the block; if the miner mining the block at H+1 instead chose b2, then b2 is the block on the main chain.
The community agrees on the "longest" chain. Sometimes there are multiple chains of equal length, and that's why it can be important to wait for a number of block confirmationsblock confirmations, before considering that a transaction will not be undone by a chain reorganization.