The movie opens in Brooklyn with Larry Daley (Ben Stiller) going to his car to discover it has a boot on the tire. He then runs to his friend to ask where his son Nick (Jake Cherry) is. His friend tells him that his ex-wife's new boyfriend took him home. Larry returns to his ex-wife's house only to reveal that he may get evicted soon and move to Queens. He then takes Nick to play ice hockey. After the game is over, they walk in the park and Nick reveals that he doesn't want to be a hockey player anymore, but a bond trader like his mother's boyfriend. Nick asks if he is really moving again and Larry, feeling bad about Nick, is determined to get a new job.
Cut to Larry at a job office. He is being interviewed by a woman who could possibly give him a job somewhere. She tells Larry that she has never seen a resume like his. He thanks her, only to discover that it wasn't a compliment. They discuss different things about his resume including his invention, the "snapper," which is a spin-off of the clapper for lights. She is about to dismiss him when he explains to her how much he really needs a job. After a small pause, she sighs and gives him the address to the Museum of Natural History, saying that everybody she sent over there got turned down, but he "might get lucky."
Cut to Larry walking in the Museum of Natural History. He walks up to the desk clerk to ask for a man named Cecil (Dick Van Dyke). She indicates that Cecil should be in his office and introduces herself as Rebecca (Carla Gugino). She shows Larry around the room a bit before Dr. McPhee (Ricky Gervais) interrupts to exclaim that children are touching the exhibits. Rebecca goes on with her work while Larry proceeds to Cecil's office.
Larry knocks on the door of the security office and Cecil answers, letting him in. He explains to Larry that the museum is losing money and they are going to fire him and his two co-workers Gus (Mickey Rooney) and Reginald (Bill Cobbs) and that they are looking for one new guard to replace them. Larry is unsure he should take the job since they are night guards and he doesn't like the idea of staying up all night in a museum. He eventually agrees and Cecil tells him to go wait outside and he will catch up with him to show him around the museum. After Larry leaves, Reginald asks Cecil if he thinks "he is the one." Cecil responds by saying, "Oh yeah, he's the one."
He begins by showing Larry the diorama room with the Mayan culture, the Roman culture, and the Western culture. After Larry leaves the room, he cannot find Cecil, he calls his name a few times and we see a figure following him. He taps Larry's shoulder and Larry screams. The figure takes off his mask to reveal Cecil dressed in Indian clothing. Cecil laughs because he scared Larry but reminds him not to mess with anything. Moving on, Cecil shows him Attila the Hun, the Easter Island Head, and the Hall of African Mammals. Larry notices the monkey and Cecil says that they call the monkey Dexter. After they leave that room, they enter the Pharaoh's Chamber, and Cecil shows Larry the Pharaoh's tomb and the tablet of Ahkmenrah, which he reveals is 24 karat gold.
Larry is about to start his first day of work and the three guards are there to give him what he needs. Cecil gives him his keys to lock the gates and his flashlight. He also pulls out an instruction manual and gives it to Larry. Cecil tells him to turn a few lights on since it gets a little spooky in there. He also tells him in a very serious tone not to let anything in or out. Larry is confused by this but goes on working. He turns on the lights and sits at the front desk, blowing into the telephone which echos his voice throughout the entire museum.
After awhile, he falls asleep, but wakes up by falling out of his chair. He gets up and walks across the room to find that the Tyrannosaurus Rex is missing. He calls out Cecil's name thinking that it is Cecil pulling another prank on him. When he hears something down the hall, he clicks on the flashlight to find the dinosaur drinking from the water fountain. In shock, Larry drops the flashlight, the dinosaur hears this and stops drinking. He roars and runs towards Larry, who tries to run and hide by ducking under the front desk. He calls Cecil, who tells him to read the instructions and hangs up. The dinosaur picks up the desk with it's mouth and throws it across the room. Larry pulls out the instruction manual and reads the first instruction which is to "Throw the bone." Larry is confused until a large rib bone drops in front of him and Larry picks it up and hurls it across the room. Larry then tries to run out, but the T-Rex appears in front of him, drops the bone, and pushes it to Larry, suggesting he wants it to be thrown again. Larry realizes that the dinosaur wants to play fetch and he throws the bone again. This time the T-Rex's tail hits Larry and throws him onto the stairs. Larry then flees the room.
As Larry is running, he passes by Cavemen who are trying to create fire. He runs into the Easter Island Head who calls him Dum-Dum and that Larry is supposed to bring him "gum-gum." The head warns Larry to run as Attila the Hun shows up and chases Larry into the elevator. Larry goes into the room where Sacagawea is on display and he asks her what is going on. Being behind the glass, she cannot hear him. Larry falls backwards and a Civil War soldier points a gun over Larry's neck and fires at other soldiers. Larry manages to escape unharmed.
Larry goes up the elevator into the Hall of African Mammals. He reads the next instruction which is to lock up the lions, "or they'll eat you". He drops the manual and it sails into the middle of the room, right in front of the lions. Larry runs for the manual, quickly runs through the trees and closes the gate behid him. He reads the next instruction which is to double check his belt because the monkey probably stole his keys. He checks and his keys are missing. He looks up to see Dexter holding his keys. As Larry tries to get them back, Dexter bites his nose and pees on him. Dexter then steals the instruction manual and rips it in half. Furious, Larry goes and sits in the diorama room. He then sees small Mayan warriors and they start shooting small arrows into his hand. Larry runs away in pain.
Larry sees the Western exhibit and stands in front of it. He hears a noise below him and looks down to see that some small western people have tied his feet together. He turns around, but they push him off his feet onto the train tracks in the exhibit. Just then we see Jedediah (Owen Wilson) yelling at the cowboys to hog tie him. The cowboys throw ropes over his body and secure them tightly, holding Larry down on the train tracks (actually, only his head rests on the tracks). They reveal that they are tired of being locked up in the exhibit every night and Jedediah gives the order to start the train. Larry begs Jed to stop the train, but Jed refuses. The train hits Larry in the head, but derails without doing much damage. Larry breaks out of the ropes and gets up.
We see an army of small Roman soldiers ready to attack Larry. Jedediah reveals the leader to be Octavius (Steve Coogan). He tells the army to unleash hell, and they start shooting fireballs at Larry. He runs away and is rescued by Teddy Roosevelt (Robin Williams). Jedediah yells to him that Teddy will not always be there to protect him and they ride away on his horse. When they stop in the main room, Larry asks him why everything is alive and Teddy takes him to the Pharaoh's Chamber. We see the Pharaoh struggling in his sarcophagus and Teddy yells that he's not getting out. He points to the tablet and tells Larry that the tablet is why everything comes alive at night. He tells Larry that it is his job to make sure that nothing gets out of the museum, otherwise they will turn to dust if the sun comes up while they are outside. Teddy helps Larry get everyone together and we see Teddy staring through his binoculars at Sacagawea. When Larry asks why, Teddy just says that he is tracking. Larry is not sure that he will come back. Teddy tells him that some are born great, but others have greatness thrust upon them. Teddy goes back to his position in the museum and Larry starts to walk away. Jedediah pops out of Larry's pocket and threatens to shoot his gun at his eye. He shoots, but nothing comes out. Larry pulls him out of his pocket and takes him back to the diorama room, just as the sun rises, and everything goes back to the way it was.
Cecil and the other security guards go into the museum and Larry jumps out and surprises them. He tells them that everything in the museum comes to life at night. Cecil explains that if they told him, he never would have believed them. Larry quits and exits the museum. On the museum steps, Larry encounters Nick on his way to school. Nick tells him that he loves that Larry works at the museum. Larry promises to give Nick a tour sometime. Now Larry must go back inside and get his job back. Dr. McPhee calls Larry to the diorama room to find that a Roman figure is in the stockades in the Western Culture diorama. Thinking that Larry did it, Dr. McPhee warns him not to try anything like that again. Larry runs back to Cecil and asks him if he has another instruction manual since Dexter ripped his apart. Cecil does not have another copy but advises Larry to read about history, that it would help. Larry sees Rebecca giving a tour to some kids and decides to join them. He laughs when Rebecca says that the monkey is known for it's "generous and loving nature". He calls her aside to ask her if Sacagawea was deaf (since she could not hear him the night before). Rebecca is annoyed by his interruptions and asks why he is following them. Larry says that he just has some questions and gets Rebecca to agree to meet him later for a cup of coffee to discuss history. Rebecca reveals that she is working on a dissertation about Sacagawea and admires her a lot.
In the library, we see Larry reading books and searching on the computer for information about things in the museum. He discovers that Attila the Hun liked sorcery, and would tear the limbs of his enemies off. Back at the museum, Larry is about to get ready for his shift when Cecil and the other security guards come in to wish him good luck and to say goodbye. They are leaving the museum for the last time. Cecil asks Larry to walk them out, but Reginald stays behind. Cecil says it's because Reginald is very emotional. While Reginald lingers, he makes a clay mold of Larry's key. Larry goes off to begin his shift.
Larry begins by ripping off one of the T-Rex's rib bones and attaching it to a remote controlled car to keep T-Rex entertained while chasing it. He goes to the Cavemen, still looking for ways to create fire, and throws them a lighter. He goes over to the Easter Island Head and gives him the 'gum-gum' that he asked for yesterday. He goes into the diorama room. He puts on a mit to protect himself from the arrows the Mayan people will throw while he closes the glass to their diorama. He moves to close the glass to the Roman Cuture diorama when he sees them breaking down their wall to expand. He moves over to the Western Culture diorama to see that Jedediah is giving the order to blow the mountain with dynamite. Larry tells him not to since all he will hit is a wall. Jed tells them to detonate it anyway, but only a little poof of smoke results. Larry asks them what they are doing and Jedediah responds by saying that they are working on Manifest Destiny. Larry, fed up, grabs Octavius and Jedediah and tells them that he is going to let both their cultures roam free if they promise to behave. They both promise and Larry exits the room. Larry then proceeds to the Hall of African Mammals and tells Dexter to go inside. Dexter goes into the room and Larry locks the door. Dexter thinks he stole the keys and holds up a set of baby keys. Larry locks the gate in triumph thinking he has outsmarted Dexter. He continues his rounds and finds Teddy looking at Sacagawea again. Larry advises Teddy to just go and say hello to her. Larry leaves, and Teddy goes up to the glass about to say hi to her, but walks away, too shy. Cut to Larry's office being broken into by an unknown figure; he opens Larry's desk and plants various artifacts from the museum, then leaves.
Larry encounters Attila the Hun in the Teddy Roosevelt Rotunda. Attila moves to attack, but stops short when Larry tells him that he can show him some magic. Attila watches as Larry makes a bouquet of flowers appear, makes a coin disappear and reappear out of Attila's ear and makes a handkerchief disappear. However on the last trick he messes up, revealing part of the handkerchief sticking out of the fake thumb he is wearing. Attila discovers this and tells his Huns to grab Larry and start pulling his limbs apart. However, they are interrupted by an incoming elephant. Larry looks up and discovers that the animals have escaped from the "locked" Hall of African Mammals and we see Dexter holding Larry's keys. Larry chases after Dexter and they run into the diorama room where Larry sees the Romans and cowboys fighting. Meanwhile, Dexter uses the keys to open a window. Larry goes to Jedediah and Octavius fighting on a post and snaps at them to make them stop. They explain that they will never get along and they continue fighting. Larry sees Dexter again and chases after him only to find one of the cavemen on fire. He takes the fire extinguisher and sprays the exhibit with foam, smothering the fire. Larry then continues to chase Dexter. Meanwhile, one of the cavemen finds the open window and sees people outside around a bonfire. He leaps out the window to join them.
Larry corners Dexter in one of the exhibits and tries to get the keys back. Dexter slaps him and they begin to get into a slap fight until Teddy stops them. Teddy gets the keys back by respectfully appealing to Dexter. Larry tells Teddy that he can take over; Larry has decided he's not coming back (again). Teddy tries to convince him to stick it out, but he is unsuccessful. Christopher Columbus points Larry to the open window. Larry runs to the window and sees a caveman outside and the sun rising. Larry runs to the front door to see the caveman coming back but too late; the sun comes up and the caveman turns to dust in the street. A street cleaner drives by and cleans his remains off the road.
It is morning and Nick and two friends go to the museum thinking that they will get a free tour. Larry goes to clean up the foam residue from the caveman display, but Dr. McPhee fires Larry right in front of Nick and his friends. Nick and his friends leave without Larry seeing them. Larry runs after Dr. McPhee and convinces him to give him one more night. Back at his mother's apartment, Nick tells Larry that he was at the museum and saw him get fired. Larry tells Nick that he didn't get fired and tells him that he can go to work with him that night. At the start of his evening shift, Larry sees Rebecca and asks her how her dissertation is going. She says that she has given up because she will never know who Sacagawea really was. Larry thinks for awhile and decides to tell Rebecca the secret. Rebecca, thinking that Larry is making fun of her, leaves the museum.
Nick arrives at the museum. Larry sits Nick down on the front desk and tells him that the dinosaur is about to come to life. It is the time things usually animate, but nothing comes to life. Nick is upset and thinks his dad is lying to him. Larry takes him to the Pharaoh's chamber to discover the tablet is missing. Larry runs to where the tablet should be and turns around to see that Nick is gone. He runs out and sees Nick going down the stairs to leave. Then they see a shadow of someone outside; they proceed to investigate. Nick finds a shopping cart containing the tablet and he removes the tablet. Cecil and the other security guards come out of the darkness and tell Nick to give them the tablet. Larry tells Nick to turn the middle square on the tablet, which will make everything come alive. Nick, conflicted about whom to believe, eventually turns the middle piece. The three guards prepare to fight. Larry tells Nick to take the tablet and run. Gus starts to run after him, but Larry stops him and gets knocked to the floor. Larry gets up, and Gus punches him twice. Cecil, kicks him to the floor again and explains how they loved working there. They explain that the tablet also gave them new life. When they found out they were going to get fired, they had to do something. They reveal that they planted the goods in his desk to frame him. They leave to find Nick while Gus delivers a piledriver on Larry's back, almost knocking him out.
Meanwhile, Dexter opens the front door to the museum, letting everything out. Nick runs into the Pharaoh's Chamber and Cecil takes the tablet. Larry runs in and Cecil knocks him over, stealing his keys and locking him and Nick in the Chamber. Larry calls out for Teddy, who comes and introduces himself to Nick. Larry asks Teddy to get him out, but he declines, telling Larry that it is "his moment"; Teddy rides away. Larry turns around, and the giant Anubis statues are about to attack Larry and Nick. Larry manages to run through the statues' defense and opens the Pharaoh's Tomb. The Pharaoh orders the Anubis to stand down and unwraps himself. He introduces himself as King Ahkmenrah. The Anubis free them from the Chamber allowing the three to go to find the tablet.
They find the entire museum battling one other in the Teddy Roosevelt Rotunda. Attila the Hun sees Larry across the Rotunda and runs to attack. Larry decides to meet force with force. They meet and yell at each other until Larry finds Attila's soft side and eventually gets Attila on his side. Larry then tries to get everyone's attention, but fails. The Easter Island Head then screams at everyone to be quiet and Larry is able to speak. He explains how the guards stole the tablet and gives everyone a task to get it back. He tells the Civil War soldiers to go around the Planetarium wing. He tells Columbus to take the Cavemen and go around to the reptiles section. He tells Jedediah and Octavius to go take care of Cecil's van. They refuse to work together at first, but Larry convinces them to go by saying that they aren't that different; they are both great leaders. He convinces everyone to work together and they proceed.
Outside, Jedediah and Octavius take some of their people and go to flatten the tires on the van. The air pressure blows everybody off the air valve, except Jedediah and Octavius. Meanwhile, the Civil War soldiers catch Gus, and Columbus and the cavemen catch Reginald. Cecil, who is holding the tablet, gets to the van and drives away. Larry, Nick and Ahkmenrah go outside to find the van gone. Larry goes to get Sacagawea to help track Cecil. She finds that he crashed his car across the street and went back into the museum.
Suddenly, Cecil appears out of nowhere with a team of horses and a carriage. He is about to run her over when Teddy appears and pushes her out of the way. Sacagawea is fine, but Teddy has been cut in half. Larry takes Teddy's horse and chases after Cecil while Sacagawea stays behind to help Teddy. Jedediah and Octavius also appear in the RC car with the bone still attached; they lead the T-Rex onto the road with Larry. Jedediah turns the car so that T-Rex will collide with Cecil, but the car spins out of control and flips behind a small hill, exploding with a puff of smoke.
Larry rides next to Cecil and warns him to stop the horses or he will. Cecil explains how the horses are money carriers and were trained not to stop for anything but a secret word. Larry reveals the word to be Dakota and the horses stop, sending Cecil flying into the air. Larry grabs the tablet and takes Cecil to Attila the Hun. Attila takes Cecil back to the museum. Larry gives Ahkmenrah the tablet and he uses its powers to send everyone back to the musem. Larry also calls Rebecca and tells her to go to the museum.
Everyone goes back to the museum and Larry and Ahkmenrah do a count to see if anyone is missing. Teddy comes back, having been fixed and Rebecca walks in. She sees Sacagawea and walks off with her, asking her questions. Larry and Teddy finish the count and Larry realizes that two people are missing, Jedediah and Octavius. He assumes they are dead until Nick tells him to look and they see them both struggling up the steps to the museum. Jedediah responds by saying, "You ain't getting rid of us that easy." They salute and proceed into the museum.
Teddy says he will see Larry tomorrow night, but Larry is not sure that he will still be working there; the main hall is a mess from all the fighting. Morning comes and Larry is in Dr. McPhee's office watching several news reports of the incidents last night. Dr. McPhee fires Larry again. They both walk out and see that the museum is filled with people and extremely busy. A chastened Dr. McPhee gives Larry his job back by simply handing him his flashlight and keys. That night, Larry is at the museum and the "night crew" is partying in the Rotunda. We see everyone getting along and Nick is riding on T-Rex who is being guided by Jedediah and Octavius with their new toy sports car. Larry then flips his flashlight, puts it in his belt, and snaps his fingers to turn the light off-- he's completed a model "snapper" for himself.
During the credits is a scene showing that Cecil, Gus and Reginald have been re-hired as custodians and janitors for the museum. It's not what they'd hoped for, but they make the most of it.