A consensus mechanism is a protocol that lets nodes agree on the state of a blockchain without a central authority. The most famous is Proof of Work, used by Bitcoin. Miners solve computationally difficult puzzles. The first to solve it gets to add the next block. This proof of work makes it expensive to attack the network. You'd need to control 51 percent of computing power.
Proof of Stake, used by Ethereum, works differently. Validators stake tokens as collateral. They propose blocks. Other validators attest to the blocks' validity. If validators misbehave, they lose their stake. This makes attacks expensive, you'd need to own 51 percent of staked tokens. Proof of Work uses massive amounts of electricity. Proof of Stake is more energy efficient.
Other consensus mechanisms exist, Proof of Authority where approved entities validate blocks, Proof of History where there's a cryptographic timestamp, Proof of Burn where validators burn tokens. The choice of consensus mechanism determines network security, energy efficiency, and decentralization. Proof of Work is highly decentralized but energy-intensive.
Proof of Stake is more efficient but has centralization concerns if wealth concentrates. There's no perfect consensus mechanism. Every design trades off between security, decentralization, and efficiency.