A man (Jake Gyllenhaal) wakes up on a train sitting across from a woman named Christina Warren (Michelle Monaghan). The woman knows him by the name Sean Fentress, but he doesn't seem to know her and appears uncertain of his own identity. After eight minutes, a bomb goes off on the train, and the man awakens to find himself strapped inside a small geodesic dome. There, Air Force Capt. Colleen Goodwin (Vera Farmiga) explains to him through a computer screen that he is actually Colter Stevens, a decorated army helicopter pilot, now on a mission to locate the maker of a bomb which destroyed a train headed into Chicago. This is to be accomplished using the Source Code, a time loop program that allows him to take over someone's body in a reenactment of their last eight minutes of life.
Stevens has no memory of how he became involved in the Source Code project; his last memory is of flying in a recent mission in Afghanistan while taking on enemy gunfire. Stevens's mission in the Source Code is to assume the identity of Fentress, one of the train's passengers, locate the bomb, discover who detonated it, and report back to Goodwin to prevent the bomber from detonating a larger dirty nuclear device in downtown Chicago, which could cause the deaths of millions of people. Goodwin and the Source Code's creator, Dr. Rutledge (Jeffrey Wright), tell him that the Source Code is not a simulation, but a visit into the past in the form of an alternate reality. He's told that he cannot truly alter the past to save any of the passengers, but that he must gather intel that can be used to alter the future and prevent a future attack. Goodwin assures Stevens that "everything will be OK."
Stevens is sent into the Source Code repeatedly, and each time a series of events repeat, with variations due to Stevens's acts. Meanwhile, he goes on searching for the terrorist and getting to know Christina, until the bomb that destroys the train goes off and Fentress dies, sending Stevens back to the chamber.
Even though Rutledge and Goodwin constantly direct him to focus on finding the bomber, each time around he learns more, both about the terrorist attack and his real life personal situation. Stevens resolves to complete his mission, now with the added personal goal of saving Christina and the people on the train if at all possible.
Using a cellphone while on the train, Stevens eventually discovers that he supposedly died two months ago. He confronts Goodwin with this information, and learns from Goodwin and Rutledge that he had been shot down during the war, and his mutilated body was appropriated by the Air Force and used by Rutledge to operate the Source Code. Stevens then asks Rutledge and Goodwin to let him die, which they promise they will do once the mission is finished.
He eventually discovers that the bomber is a disturbed American young man named Derek Frost (Michael Arden), which allows authorities to apprehend Frost and save Chicago before the second bomb is detonated.
With the mission accomplished, and despite their promise, Rutledge orders Stevens's memory to be erased and stored for reuse in further missions. However, Stevens persuades Goodwin to send him in one more time and give him one last chance to avert the train disaster. Goodwin agrees that he deserves to be allowed to die in peace afterwards instead of being held alive as a military brain in a vat.
With the information he has uncovered from previous trips into the Source Code, Stevens is able to defuse the bomb and immobilize Frost before he can destroy the train.
Stevens and Christina kiss in the last seconds before the eight-minute mark. In that instant, Goodwin turns off his life support per his request, but to Stevens's surprise, his mind remains in Sean Fentress's body. He is then able to safely leave the train with Christina, also saving the rest of the passengers. Now in the alternate universe, an alternate version of Goodwin receives an email from Stevens explaining what has happened and that the Source Code can alter the target alternate reality. The movie ends with Stevens taking Fentress's place, and asking Goodwin to help Captain Colter Stevens and tell him that "everything will be OK.", which Goodwin had told Stevens earlier in the film.