Talking about bitcoin’s blockchain; when a miner is trying to alter the ledger, other miners following the correct rule-see this and decline to accept the change. And as a result, the bad guy's work is pointless. At least 20% of the hashrate is now owned by publicly traded corporations, making it impossible for a miner with 51% of the hashrate to launch an attack quickly in a matter of seconds.
The 51% attack and you mentioned is about replacing an older blocks with another valid block meaning they both follow the same "correct rule" since they are both valid. You can't reject such blocks. Also technically it is not the miner rejecting invalid blocks, it is the node.
The reason why 51% attack doesn't work is because how decentralized the mining in bitcoin is. It is not possible to gain control of that much hashrate.
These days it's nearly impossible to perform that 51% hash rate attack after looking at the hardware different people are using and not at the same place.
I 'd like to know in 140 yrs when bitcoin wouldn't be mineable then how would this 51% attack be still work?, I mean at that time there will be no blocks to mine only transaction fees. but then transactions wouldn't be inside blocks.