U.S. Army Aviation pilot Captain Colter Stevens (Jake Gyllenhaal), last aware of being on a mission in Afghanistan, wakes up on a commuter train to Chicago, at 7:40 am. To the world around him – including his traveling partner Christina Warren (Michelle Monaghan) and the bathroom mirror – he appears to be Sean Fentress, a school teacher.
As he comes to grips with this revelation, the train explodes, killing everyone aboard. Stevens regains consciousness inside a dingy dim cockpit, leaking oil. Communicating through a video screen, Air Force Captain Colleen Goodwin (Vera Farmiga) verifies Stevens' identity, and insists he stay "on mission" to find the bomber before another larger "dirty bomb" hits downtown Chicago in six hours.
Inside the "Source Code" experimental device designed by scientist Dr. Rutledge (Jeffrey Wright), he experiences the last eight minutes of another compatible person's life within an alternative timeline. Stevens is unwillingly sent back into the Source Code again and again in frustrating, exhausting attempts to learn the bomber's identity. He tries to warn authorities on the train and flee with Christina, escaping the explosion.
Other times he cannot locate or disarm the bomb and dies on the train. But Rutledge insists the alternate timeline is not real. It is revealed that he has been "with them" for two months since being reported killed in action in Afghanistan. He is comatose and missing most of his body, hooked up to neural sensors. The cockpit capsule is in his imagination, his brain's way of making sense of a missing environment.
A confused and frustrated Stevens asks, "As one soldier to another, am I dead?" Angry to learn that he is on life support, he asks to be disconnected after the mission. Rutledge agrees. Stevens catches the bomber Derek Frost (Michael Arden), who leaves his wallet behind to fake his own death, and gets off at the last stop before Chicago. In one run-through, Frost kills both Fentress and Christina, and flees in a rented white van.
Stevens remembers the license number and direction so the authorities can catch the terrorist. But Rutledge reneges, orders Goodwin to wipe Stevens' memory for a future mission. Stevens convinces Goodwin to allow one more try, to save everyone on the train, despite Rutledge's insistence that everyone on the train had already been killed in the explosion.
Stevens is sent back into the Source Code where he disarms the bomb, subdues Frost and handcuffs him to a handrail inside the train. He reports the bomber and the bomb to authorities, composes an email to Captain Goodwin, then calls to reconcile with his estranged father under the guise of a fellow soldier. He asks Christina what she would do if she knew that she only had seconds left to live, and starts to kiss her.
At the same time, Goodwin approaches the air-tight chamber with the torso of Steven's comatose mutilated body, and disconnects the life support. Rutledge bangs on the outer door in vain. Surprisingly, Stevens finishes the kiss with Christina, revealing that the alternate timeline of the Source Code was indeed real, contrary to what was proposed by Rutledge. They continue on the train, and then walk around downtown Chicago facing the Cloud Gate.
Later that morning, the alternative-timeline Captain Goodwin arrives for work at Nellis Air Force Base and receives the email from Stevens. While news breaks about the failed bomber on the Chicago train, he informs Goodwin that they have changed history, and Goodwin seemingly recalls something. He asks her to reassure this timeline's Stevens that "everything is gonna be okay."