The fact is, Optimus Prime is not a good guy. That's not saying he doesn't have the best of motivations in his own mind, but then I'm sure so did any other countless historical figures who committed, let’s say, dubious acts. He paints himself as the liberator of his people, fighting for their rights, and yet his force of Autobots is tiny in comparison to the Decepticon army. It is almost as if Optimus and his Autobots are a small terrorist cabal who have broken away and are doing everything in their power to destroy a Cybertronian society they see as corrupt.
I mean, look at the evidence. In the first Autobot incursion at Mission City, we saw an enormous battle as the Autobots took on the Decepticons for possession of the Allspark Cube. All evidence suggests that if the Decepticons had got to the Cube first, they'd have just flown off into space with it to restore their world and left us in peace.
The Cube's purpose is to create life, and it is the only thing that can be used to restore Cybertron. So what does Optimus Prime do when it looks like he has lost the fight? He orders the Cube destroyed.
Then we get to the second incursion, in Egypt. Autobot zealots have in their possession the Matrix, which is another device capable of creating life and restoring their world, and they use it entirely on restoring Optimus Prime to life. Optimus Prime then goes on to destroy the Sun Harvester device, and tear off the face of The Fallen, an ancient Decepticon leader, before murdering him. In my book, heroes don't tear off people’s faces using hooks. That's for the villains. And yes, clearly it was slightly douchy of the Decepticons to try and use the Sun Harvester on our sun as there are billions of suns in the galaxy, but it doesn't mean that Optimus Prime needed to destroy it too. It's like he doesn't care.
I mean, he doesn't care, does he? Every time he has come across some way to save his world, he does everything in his power to destroy it. Do we need to wonder why Megatron seemingly has an unending army of Cybertronians determined to stop him and Prime can only gather up a ragtag bunch of extremely violent individuals?
Then we get to Chicago. Yes, the Decepticons have made a complete mess of the city, but this can equally be blamed on Prime. He states quite clearly that he knew of Megatron's intentions, but wanted to "teach the humans a lesson" by vanishing and letting Megatron burn the city. There was no tactical advantage to Prime turning up late, as Decepticon long range sensors would have picked up his approach from miles away. He just wanted to teach the humans a lesson.
Then Optimus Prime shears Cybertron in half.
I mean, if you ever thought he 'had it', by that point he'd lost it. On the one hand he professes to be a great patriot, and on the other hand he and his band of rebel Autobots do everything in their power to wipe their race out, the coup-de-grace being to destroy their homeworld. It's almost as an afterthought.
So of course Optimus Prime executes Sentinel Prime. It is just a continuation of his extremist rampage against his own people. Of course when Megatron offers the hand of friendship, Optimus rips out his spine.
We can understand Megatron. We may not like him, but we understand him. To him, humanity is no more than ants, or vermin, and a property developer doesn't angst much about paving over a piece of woodland to build his house. But in Chicago, if Megatron wanted Prime dead, Prime would be dead. He could have waited for the Optimus/Sentinel battle to finish and then kill the victor. There would be no need for future betrayal. Instead Megatron realised the true way forward for their world was in cooperation between adversaries. But Optimus did not want any way forward for their world.
Because we can understand Optimus, too. And it isn't in those warm fuzzy cartoons and comic books they keep pushing at you. It's in the actions of a war criminal. It's in the actions of a terrorist extremist who has somehow won our heart and minds while committing the most abominable acts.
[Fun fact: the ending was changed late in the day, and so the original was kept in the comic/novel of the film. Originally Optimus accepts Megatron's truce rather than tearing out his spine]