Farhan Qureshi (R. Madhavan), Raju Rastogi (Sharman Joshi), and Rancchoddas "Rancho" Shyamaldas Chanchad (Aamir Khan) are three engineering students who share a room in a hostel at the fictional Imperial College of Engineering, one of the best colleges in India. While Farhan and Raju are mediocre students from modest backgrounds who want to lift their families out of poverty, Rancho is a wealthy genius who studies for the sheer joy of it. However, Rancho's passion for knowledge and machine-building rather than exam rankings, conforming to the system and social climbing, incurs the wrath of Professor Viru Sahastrabudhhe (ViruS) (Boman Irani). Rancho irritates his lecturers by giving creative and unorthodox answers, and confronts ViruS after fellow student Joy Lobo hangs himself in his dormitory room. Joy had requested an extension on his major project on compassionate grounds—his father had suffered a stroke—but ViruS refused, saying that he himself was completely unmoved by his own son's accidental death after being hit by a train. The professor then proceeded to phone Lobo's ill father and inform him that his son will not be graduating. Rancho denounces the rat-run, dog-eat-dog, mindless rote learning mentality of the institution, blaming it for Lobo's death, after which ViruS hauls his rebellious student and forces him to lecture the other students. Rancho then embarrasses the staff and students by asking them questions about non-existent concepts (Farhanitrate and Prerajulization), prompting them to feverishly search through textbooks trying to find a definition.
Threatened by Rancho's talent and free spirit, ViruS labels him an "idiot" and attempts on a number of occasions to destroy his friendship with Farhan and Raju, warning them and their parents to steer clear of Rancho. In contrast, ViruS’ model student is Chatur Ramalingam or "Silencer", (Omi Vaidya) who sees a high rank at the prestigious college as his ticket to higher social status, corporate power, and therefore wealth. Chatur conforms to the expectations of the system, and works by mindless rote learning, and unethical means of destroying his competition, such as trying to distract his classmates by feeding them pornography magazines. Rancho humiliates Chatur, who is awarded the honour of making a speech at an award ceremony, by substituting obscenities into the text, which has been written by the librarian. Chatur mindlessly memorises the speech, without noticing that anything is amiss, directing frequent vulgar language at ViruS and the federal education minister, much to their horror, and to the students' uproarious laughter.
Meanwhile, Rancho also falls in love with ViruS' medical student daughter Pia (Kareena Kapoor) when he, Raju and Farhan crash her sister's wedding banquet in order to get a free meal. This incident provokes further run-ins with ViruS, but Rancho wins over Pia after first insulting her fiancee Suhas and then demonstrating that Suhas only sees Pia's hand in marriage as a means of furthering his career by marrying into an influential family. Rancho does this by damaging Suhas's expensive material goods, and then confiscating Suhas's gifts to Pia. The latter incident provokes an angry tirade from Suhas towards Pia about how much money she has lost him—he thinks that she was absent-minded and lost the present. After this, the pair fall in love and Pia saves Raju's ill father by administering emergency treatment after he collapses.
Meanwhile, the three students continue to anger ViruS, although Rancho continues to come first in every exam, while Chatur is always second, and Farhan and Raju are inevitably in the last two positions. The tensions come to a head when the three friends, who are already drunk, break into ViruS's house at night to see Pia, and then urinate on a door inside the compound before running away when ViruS senses intruders. The next day, ViruS tells Raju that he is being expelled. This devastates Raju, who is afraid that his ill father will die of the shock of his only son being cast aside and being unable to support the family and earn a dowry for his unmarried sister. ViruS sees an opportunity to break Raju's friendship with Rancho by offering to expel the latter instead. Unable to choose between betraying his friend or letting down his family, Raju jumps out of the window and lands on a courtyard, but after extensive care from Pia and his roommates, awakes from a coma.
The experience has changed Farhan and Raju, and they adopt Rancho's outlook. Farhan decides to pursue his love of photography, while Raju is unexpectedly given an interview for a corporate job. He attends in plaster and a wheelchair and gives a series of non-conformal and frank answers. After turning Raju down, the stunned recruitment personnel change their mind and tell Raju to name whatever salary he wants. However, ViruS is unsympathetic and vows to make the final exam as hard as possible so that Raju is unable to graduate. Pia hears him and angrily confronts him, and when ViruS gives the same ruthless reply he gives to his students, she denounces him in the same way that Rancho did over the suicide of Lobo. Pia reveals that Viru's son and her brother was not killed in an accident but committed suicide in front of a train and left a letter because ViruS had forced him to pursue a career in engineering over his love for literature; ViruS always mentioned that he unsympathetically failed his son on the ICE entrance exams over and over to every new intake of ICE students. After this, Pia walks out on the family home, and takes ViruS's spare keys with her. She tells Rancho of the exam, and he and Farhan break into ViruS's office and steals the exam and give it to Raju, who with his new-found attitude, is unconcerned with the prospect of failing, and refuses to cheat and throws the paper away. However, ViruS catches the trio and expels them on the spot. However, they earn a reprieve when Viru's pregnant elder daughter Mona (Mona Singh) goes into labour at the same time. A heavy storm cuts all power and traffic, and Pia is still in self-imposed exile, so she tells Rancho to deliver the baby in the college common room with the help of a webcam, after Rancho restores power using car batteries and a power inverter that Rancho had dreamed up and ViruS had mocked. Rancho then delivers the baby with the help of a cobbled-together Vacuum extractor.
After the baby is apparently stillborn, Rancho resuscitates it. ViruS reconciles with Rancho and his friends and allows them to take their final exams and they graduate. Rancho comes first and is awarded ViruS's pen, which the professor had been keeping for decades before finding a brilliant enough student to gift it to.
Their story is framed as a flashback from the present day, ten years after Chatur vowed revenge on Rancho for embarrassing him at the speech night and promised to become more successful than Rancho a decade later. Having lost contact with Rancho, who disappeared during the graduation party and went into seclusion, Raju and Farhan begin a journey to find him. They are joined by Chatur, now a wealthy and successful businessman, who joins them, brazenly confident that he has surpassed Rancho. Chatur is also looking to seal a deal with a famous scientist and prospective business associate named Phunsukh Wangdu. Chatur sees Wangdu, who has hundreds of patents, as his ticket to further social climbing. When they find Rancho's house, they walk into his father's funeral, and find a completely different Rancho. After accusing the new man of stealing their friend's identity and profiting from his intellect, the host pulls a gun on them, but Farhan and Raju turn the tables by seizing the father's ashes and threatening to flush them down the toilet. The householder capitulates and says that their friend was a destitute servant boy who loved learning, while he, the real Rancho, was a lazy wealthy child who disliked study, so the family agreed to let the servant boy study in Rancho's place instead of labouring. In return, the real Rancho would pocket the qualifications and the benefits thereof, while the impersonator would sever all contact with the world and start a new life. The real Rancho reveals that his impersonator is now a schoolteacher in Ladakh.
Raju and Farhan then find Pia, and take her from her wedding day to Suhas by performing the same tricks with his material possessions, and having Raju turn up to the ceremony disguised as the groom and eloping with Pia in front of the guests. When they arrive in Ladakh, they see a group of enthusiastic ethnic Tibetan children who are motivated by love of knowledge. Pia and the fake Rancho rekindle their love, while Chatur mocks and abuses the schoolteacher before walking away. When his friends ask what his real name is, he reveals that it Phunsukh Wangdu and phones Chatur, who has turned his back, to turn around and meet his prospective business partner. Chatur is horrified and falls to his knees, trying to flatter Phunsukh Wangdu.