As part of the Thirteen's plan to defeat Unicron, Optimus Prime exposed his spark, its frequency almost identical to Primus's own, to Unicron's Anti-Spark, the resulting interaction between the two knocking both beings into stasis lock. After being poisoned by Dark Energon, Primus inserted an untainted portion of his spark into the Matrix of Leadership which he bestowed upon Optimus Prime.
The shard managed to keep him alive after being at ground zero of a Space Bridge explosion, before another shard, plunged directly into his spark, restored his body to full functionality.
After Megatron perished, Deadlock the lasting consequence of this was revealed with Megatron's polluted spark now severed from the AllSpark and bound to Unicron's Anti-Spark, allowing the Dark God to possess Megatron's corpse.
In order to prevent Unicron from devouring the physical AllSpark, Optimus Prime merged the AllSpark with the Matrix of Leadership and thus his own spark. Unable to separate the three, Optimus willingly surrendered his life to allow new generations to exist on Cybertron. The AllSpark rejoining the Core caused it to ignite and birth a multitude of new sparks.
Before Optimus could pass into the afterlife, the Thirteen intercepted his spark and brought it to their realm. To provide Optimus with the power needed to defeat Megatronus, the Primes bestowed upon him a portion of their power, directly from their sparks. They later took it back but doing so left Optimus substantially weakened.