The ending of Transformers (2007) closes the AllSpark story and plants the seeds for the sequel. Sam rams the AllSpark into Megatron's chest, which kills Megatron and destroys the cube. Optimus Prime keeps a small shard of it, although he knows his home world cannot be brought back. The government shuts down Sector 7 and sinks the Decepticon wreckage in the ocean. Sam and Mikaela begin a relationship, and Optimus sends a signal inviting the surviving Autobots to make Earth their new home.
Why does the cube work as a weapon? In my opinion, the logic is the tidiest piece of the script. The AllSpark creates life, so putting it into the body of the strongest Decepticon is the one move that cancels both the cube and the villain at once. It is also the moment where a teenager with a car becomes the person who finishes the fight, which is exactly the boy and his car idea Steven Spielberg pushed for when he joined as executive producer in 2004. The set up pays off in a clean line: Sam started the film buying an old car, and he ends the Mission City fight as the only person able to carry the cube to Megatron.
How does it compare with the rest of the saga? The 1986 animated film ends its first big fight the other way round, with Optimus Prime defeating Megatron but being mortally wounded. Here, in my opinion, the rulebook is flipped: the villain dies and the leader survives, which is a gentler note for a PG-13 summer film. The boy and his car template also keeps coming back. In Bumblebee (2018), B-127 scans a yellow Volkswagen Beetle and a grieving Charlie Watson finds it, and in Rise of the Beasts Noah Diaz tries to steal a Porsche that turns out to be the Autobot Mirage. Age of Extinction runs a variation, with Cade Yeager finding a badly hurt Optimus in Texas. It could be argued that the 2007 film wrote the pattern the later entries keep reworking.
What does it set up? Optimus keeping a shard and calling other Autobots to Earth makes room for a larger cast, and it could be argued that the shard is the loose thread the next film picks up. In Revenge of the Fallen, set two years after the battle of Mission City, Sam finds a small leftover shard of the AllSpark, and the Decepticons revive Megatron. Perhaps that is why the first film still feels complete on its own while pointing at a franchise, and why Megatron's death turned out to be less final than it looked in the Mission City street.