Calvin Clifford (C. C.) "Bud" Baxter (Jack Lemmon) is a lonely office drudge at a national insurance corporation in a high-rise building in New York City. In order to climb the corporate ladder, Bud allows four company managers, who reinforce their position over him by regularly calling him "Buddy Boy," to take turns borrowing his Upper West Side apartment for their various extramarital liaisons, which are so noisy that his neighbors assume that he is a playboy bringing home different women every night.
The four managers (Ray Walston, David Lewis, Willard Waterman, and David White) write glowing reports about Bud, who hopes for a promotion from the personnel director, Jeff D. Sheldrake (Fred MacMurray). Sheldrake calls Baxter to his office but says that he has found out why they were so enthusiastic. Then he goes on to promote him in return for exclusive privileges to borrow the apartment. He insists on using it that same night and, as compensation for such short notice, gives Baxter two company-sponsored tickets to the hit Broadway musical The Music Man.
After work, Bud catches Fran Kubelik (Shirley MacLaine), an elevator operator on whom he has had his eye, and asks her to go to the musical with him. They agree to meet at the theater after she has a drink with a former fling. The man whom she meets, by coincidence, is Sheldrake, who convinces her that he is about to divorce his wife for her. They go to Baxter's apartment as Baxter waits forlornly outside the theater.
Several weeks later, at the company's raucous Christmas party, Sheldrake's secretary Miss Olsen (Edie Adams), drunkenly reveals to Fran that Fran is just the latest in a string of female employees whom Sheldrake has seduced into affairs with the promise of divorcing his wife, with Miss Olsen herself being one of them. At Bud's apartment, Fran confronts Sheldrake, upset with herself for believing his lies. Sheldrake maintains that he genuinely loves her but then leaves to return to his suburban family as usual.
Meanwhile, Bud accidentally finds out about Sheldrake and Fran. Heartbroken, he lets himself be picked up by a woman (Hope Holiday) at a local bar. When they arrive at his apartment, he is shocked to find Fran in his bed, fully clothed and unconscious from an intentional overdose of his sleeping pills. He enlists the help of his neighbor, Dr. Dreyfuss (Jack Kruschen), to revive Fran without notifying the authorities and sends his confused bar pickup home. To protect his job, he lets Dreyfuss believe that he and Fran are lovers who had fought, which he took so lightly that he was meeting another woman while she was attempting suicide. This comes as no surprise to Dr. Dreyfuss or his wife, who long assumed Baxter was a womanizing playboy from all the noise coming from his apartment at all hours. Fran spends two days recuperating at his apartment, while Bud tries entertaining and distracting her from any further suicidal thoughts, talking her into playing numerous hands of gin rummy.
Since she has been missing, Fran's brother-in-law Karl Matuschka (Johnny Seven) comes to the office looking for her. She has not been there and neither has Baxter. The previous day, one of the executives had seen Fran in the bedroom when he came to the apartment hoping to borrow it, and mentioned it to the other executives. Resenting Bud for denying them access to his apartment, the executives direct the man there. Baxter again takes responsibility for Fran's actions, and Karl punches him twice in the face. Fran kisses Bud for not revealing her affair with Sheldrake to Karl, and Bud, sensing that she now cares for him, smiles and says the punch "didn't hurt a bit".
Sheldrake rewards Bud with a further promotion and fires Miss Olsen for telling Fran his history of womanizing. However, Miss Olsen retaliates by telling his wife, who promptly throws him out. Sheldrake moves into a room at his athletic club but now figures that he can string Fran along while he enjoys his newfound bachelorhood. When Sheldrake asks Bud for the key to the apartment on New Year's Eve, Bud refuses and quits the firm. That night at a party, an indignant Sheldrake tells Fran about Bud refusing to let Sheldrake use the apartment, especially for bringing Fran there, and then quitting. Fran finally realizes that Baxter is the man who truly loves her. Fran deserts Sheldrake at the party and runs to Bud's apartment. Arriving at the door, she hears a loud noise like a gunshot. Afraid that Bud has shot himself, calling back to when he shared a story of his own suicide attempt, Fran pounds on the door. Bud, holding a bottle of overflowing champagne, finally opens the door, surprised and delighted that Fran is there. Bud has been packing for a move to another job and city. Fran insists on resuming their gin rummy game, telling Bud that she is now free as well. When he declares his love for her, her reply is the now-famous final line of the film: "Shut up and deal", delivered with a loving and radiant smile.