After panning across some idling diesel locomotives under the opening credits, the film begins with scenes at two rail yards in different regions of Pennsylvania run by the Allegheny and West Virginia Railroad (AWVR). In the Fuller yard in northern Pennsylvania, children arrive for a school field trip on rail safety. Meanwhile, in the southern Pennsylvania town of Stanton, Will Colson (Chris Pine) gets up for work, stopping to surreptitiously watch his wife put their son on the school bus. He calls her but she refuses to even answer.

Arriving at work, Colson, a conductor, gets his orders for the day and learns he will be working with engineer Frank Barnes (Denzel Washington), with whom he has never worked before. He goes to a group of older workers, with whom he has some barbed words over their age differences, and finds Barnes among them. While he goes to punch in, the other older workers complain about being displaced by Colson, who they say got his job through family connections in the union.

Meanwhile, in Fuller, the yardmaster yells at a pair of hostlers who have been standing idle near a train to get it moved so the schoolchildren's excursion train can get out of the yard. In a hurry, one of them, Dewey (Ethan Suplee), decides to forego connecting the air hose between the locomotive and the rest of the half-milelong (1 km) consist. This, the other hostler reminds him, means the train's air brakes will not be working, but Dewey says they will hook them up after parking the train on another track.

Barnes and Colson meet by their locomotive for the day, where Barnes, a 28-year employee, finds out that Colson, who will be in charge of the train, is only four months out of training. He reminds Colson that if there's anything he doesn't know, he should just ask. They take the locomotive out across a bridge to where they will attach their train for the day. Colson learns that a court hearing that morning did not, as he had hoped it would, end with the lifting of a restraining order preventing him from seeing his wife and son.

In the locomotive cab at the Fuller yard, Dewey turns the throttle to its highest setting to enable the train's dynamic brakes. As it approaches a switch, Dewey sees it is not set to the right track and, against the advice of the other hostler, jumps from the slow-moving train to line it properly. While he does, levers fall in the cab of their own accord. When he tries to reboard the train, it has picked up speed and he falls to the ground trying. It leaves the yard for the main line unmanned.

After some slight setbacks at the yard due to Colson's inexperience, including taking on more cars than they had in their orders, the two leave Stanton for a zinc plant. In Fuller, the hostlers let yardmaster Connie Hooper (Rosario Dawson) know that they have a runaway train headed into opposing traffic on the main line. Assuming the dead man's switch will trigger the brakes and it will be a "coaster" that stops a few miles from the yard, she calls Ned (Lew Temple), a welder for the railroad and tells him to meet the hostlers where they can get in his truck, catch the train and stop it.

By the time they get there, they realize the train is under power and going too fast to catch. Connie and the dispatchers work to get every train on the main line onto sidings. The train carrying the schoolchildren narrowly avoids a head-on collision. Michael Galvin (Kevin Dunn), Connie's superior, calls her and asks her what's happening and what she's doing about it. She hasn't figured out yet how to stop the train and begins calling the state police to make sure the grade crossings on the line are secured, since some of the tank cars on the runaway have molten phenol, a hazardous material.

On their train, Barnes and Colson hear the dispatcher's order to pull into a siding. Barnes says they can't use the siding assigned since the train is too long for it. He asks instead if a RIP track further down the line is clear.

Galvin overrules Connie's suggestion to derail the train in an area of lightly populated farmland, since it would be too costly and it is still possible to stop the train. An emergency meeting of railroad executives approves another plan, but Galvin will not tell Connie what it is. The incident becomes a media event, followed by helicopters with continuous coverage on television and reporters at crossings in small towns. The train, picking up speed, smashes through a horse trailer caught on the tracks at one junction.

The company's plan, to have a lashup of two locomotives go on the line ahead of the runaway and slow it down while another employee attempts to board the runaway's locomotive from a helicopter, fails and leads to the death of another veteran engineer. The police abort another plan, to trigger the safety switch on the locomotive's side with close-range shotgun blasts at a grade crossing, when they realize the switch's proximity to the fuel tank. Barnes and Colson make into the RIP track in the nick of time, as the runaway smashes through the back of their consist.

As it passes, Barnes sees that the coupling on the last car of the runaway is open. He decides to put the locomotive in reverse and catch the runaway. Colson, at first reluctant, joins him. Galvin insists that Connie stop them, but she refuses.

Another attempt to stop the train with derails in a small town fails because the train is too heavy and too fast. Evacuations begin as the train approaches Stanton, where it crosses the town on an elevated curve where it will derail at its current speed. If it does, it could fall into a fuel oil tank farm, causing a major environmental disaster.

Barnes and Colson catch up with the runaway. After Colson manually couples their locomotive to the train, severely injuring his foot in the process, they begin slowing it down with their own brakes but not enough. Barnes goes out onto the train and begins setting each car's brakes manually, slowing the train enough to get it past the curve without derailing. In the process the locomotive's brakes blow out and the train begins to pick up speed again.

Ned the welder catches up to the train, and Colson jumps into the back of his truck. Driving at high speed they make it to the locomotive, where Colson is finally able to get into the cab and stop the train.

In a short epilogue it is revealed that Barnes was promoted and retired, that Colson got back together with his wife, Connie was promoted to Galvin's job, and Dewey "is working in the fast-food industry".