A 51% attack on a blockchain is when malicious miners are trying to take control of more than 50% of a network’s mining power, which is computing power or hash rate, to force an altered chain of blocks to be deemed as valid.
How to prevent a 51% attack? The main safeguard from this type of attack is the decentralization of the network and its size. As long as no single entity or group controls the significant part of the mining power, the network is safe. Moreover, as a blockchain gains more nodes and hash rate, it becomes safe as it would be economically unviable to gain 51% of the network’s power for whatever reason.