There are certain continuity
issues surrounding Optimus Prime
's death as portrayed in the movie
and in the comics
In the movie, of course, he died at the hands of Megatron. After a brutal hand-to-hand battle which resulted in Megatron being knocked senseless to the ground, Optimus walked to recover his massive laser cannon and finish him off. Megatron found a pistol of some sort lying on the ground and pled with Optimus for mercy as he made his last desperate attempt. Hot Rod tried to stop him, and was overpowered and used as a human shield while Megatron unloaded round after round into Optimus's body, with a level of accuracy unknown in the cartoons. Though Optimus succeeded in beating Megatron into a condition sufficiently vulnerable to be cast into deep space by Starscream, he died on the operating table. Perceptor's words still echo eerily in the minds of many of my generation: "I fear the wounds are... fatal."
It should be noted that Optimus's inclusion in the film's blatantly unnecessarily high body count was a last minute addition to the script; it served no real purpose other than to allow Rodimus Prime to assume leadership of the Autobots - and to allow Hasbro to release the new toy line in time for Christmas.
In the comic books, however, Optimus's death occured much earlier in the (admittedly vastly different) story arc (Issue 24), and in a much more contrived manner, typical of writer Bud Budiansky.
Predictably, a new mass-energy-creation device has been created, and the Decepticons arrive to steal it, Autobots in pursuit. Rather than risk destroying the prototype, one of the research facility's staff members suggest that they settle the matter in a video game. Of course they agree, with the Protectobots, Combaticons, Megatron and Optimus wiring themselves into the system. To raise the stakes, Megatron and Optimus both wire themselves full of explosives, with the loser agreeing to die. The Autobots win the competition by acting benevolently toward the "NPC's" in the game, leaving Megatron alone to fight (and lose) against Optimus and Defensor. However, Megatron cheats, using a secret respawn code, and Optimus accidentally allows the death of a city's worth of NPC's. Due to some twisted sense of nobility, Optimus declares himself as having lost, and is destroyed as the Decepticons leave with the Hydrothermocline.