Larry Daley (Ben Stiller) is a divorced man who has been unable to keep a stable job and has failed at many business ventures. His ex-wife (Kim Raver) believes that he is a bad example to their ten-year-old son Nick (Jake Cherry), and Larry fears that Nick respects his future stepfather, bond trader Don (Paul Rudd), more than him.
Cecil Fredricks (Dick Van Dyke), an elderly night security guard about to retire from the American Museum of Natural History, hires Larry despite his unpromising résumé. The museum, which is rapidly losing money, plans to replace Cecil and two colleagues Gus (Mickey Rooney) and Reginald (Bill Cobbs) with one guard. Cecil gives Larry an instruction booklet on how to handle museum security, and advises Larry to leave some of the lights on and warn him not to let anything "in...or out".
Once night falls, Larry discovers that the exhibits come to life, including a living Tyrannosaurus skeleton nicknamed "Rexy" who behaves like a dog; a mischievous capuchin monkey named Dexter (Crystal) which steals Larry's keys and tears up his instruction booklet; rival miniature civilizations led by Old West cowboy Jedediah (Owen Wilson) and Roman general Octavius (Steve Coogan); limb-ripping Attila the Hun; an Easter Island Moai (Brad Garrett) obsessed with "gum-gum" who addresses Larry as Dum-Dum; and a wax model of Theodore Roosevelt (Robin Williams).
Roosevelt explains that since an Egyptian artifact—the Golden Tablet of Pharaoh Akhmenrah—came to the museum in 1952, all of the exhibits come to life each night. If the exhibits are outside the museum during sunrise, however, they turn to dust. Roosevelt helps Larry by restoring order, but only for one night, and Larry decides to remain as a night guard.
When Larry tells Cecil about how Dexter tore up his instructions, Cecil advises him to study history to prepare himself for his nightly duties. He also learns history from a museum docent Rebecca Hutman (Carla Gugino), who is writing a dissertation on Sacagawea (Mizuo Peck), but does not feel she knows enough about her subject.
The next night, Larry uses what he has learned to better control the exhibits. However, things go wrong and four Neanderthals set fire to a display and some other things. One of the Neanderthal turns to dust when he leaves the museum at dawn. The next morning, museum director Dr. McPhee (Ricky Gervais) almost fires Larry after what happened to the Neanderthal exhibit. He offers Rebecca a meeting with Sacagawea, but she believes that he is mocking her and the museum.
Larry brings Nick to the museum to show him the exhibits, but none of them are alive. They find Cecil, Gus, and Reginald stealing the tablet and other valuable objects. Like the exhibits, the guards receive enhanced vitality from the artifact; wishing to retain their youth, health and to fund their retirements, the three plan to frame Larry for the thefts. They disable the tablet to stop the exhibits from interfering. Nick reactivates the artifact, but Cecil locks him and his father in the Egyptian room and flees with the tablet. Larry releases Akhmenrah's mummy (Rami Malek) from his sarcophagus. The pharaoh speaks English from many years as an exhibit at Cambridge, and helps Larry and Nick escape. The three find the other exhibits fighting; Larry confronts Attila, realizing his behavior came from being hurt a long time ago, and convinces them to work together.
The exhibits capture Gus and Reginald without difficulty, but Cecil escapes by stagecoach with Larry, Nick, Akmenrah, Jed, Octavius, Rexy, and Atilla the Hun in pursuit in Central Park, where they stop him and regain the tablet. Jed and Octavius are presumably killed when their remote-controlled Hummer crashes, but they somehow survive. Rebecca sees the exhibits return to the museum before sunrise and realizes that Larry was telling the truth; he introduces her to Sacagawea.
Dr. McPhee fires Larry after seeing news reports of the strange events around the museum—such as cave paintings in the museum's subway station, dinosaur footprints in Central Park, and cavemen sightings. He rehires him after he sees that these events raised attendance. Larry, Nick, and the exhibits celebrate.
During the credits, it was shown that Cecil, Gus and Reginald weren't handed over to the authorities and are now working as janitors at the museum.