How can the blockchain keep only one version of the ledger equal across all nodes Lets say miner A is working on block X, while miner B works on block Y.
Both miner A and B find the hash for their blocks almost at the same time, and as such, they start broadcasting their results to other miners, so a consensus can be reached.

Are all the miners on the network needed to reach such consensus? 

If such, what happens if a group of miners is working to reach a consensus on a block, and some other group is working on some other block (given that the broadcast to every node takes a little bit of time)?

How do they decide what block gets added first to the blockchain?

Note that I'm pretty new to this, and still trying to learn how the blockchain works.
Each miner will take the first valid block they received and begin working on the next block with this as the previous one. They do not achieve any consensus on whether X or Y are the valid block at this stage, and some miners may be working on X, with others working on Y.

The tie is broken as soon as one of the miners creates block Z. If the previous block of Z is X, then the consensus is reached that X was the correct block and Y is discarded by miners who were working on it. Z now represents the longest chain and so all miners will begin working on this.

There may be several disputes in a row, and each miner will just work on the block they received first. The dispute will eventually be resolved because the probability that blocks will be mined simultaneously for several blocks in a row is very low.