The film opens with an image of a spinning globe and the sounds of "La Marseillaise," the French national anthem. A voiceover explains the significance of the city of Casablanca, in French-ruled Morocco. During World War II, many people wanted to escape Europe for America, and Lisbon, Portugal, became the most popular port of exit. Getting to Lisbon was not easy. A tortuous refugee path brought people to Casablanca, where they would search for an exit visa for Portugal. As the narrator finishes the description, the camera shows Casablanca's bustling market. Over the radio, the French police announce that two German officers were murdered on a train and that the murderer is headed for Casablanca. Shortly thereafter, an important German officer, Major Strasser, arrives at the Casablanca airport and is greeted by the local French commander, Captain Louis Renault. Strasser asks about the couriers, and Louis says that the murderer, like everyone else, will be at Rick's that evening.

The scene shifts to Rick's Café Americain, a stylish nightclub and casino. Ugarte approaches Rick, the bar's owner, and asks if he will hold some letters of transit for him. Rick observes dryly that the murdered German couriers were carrying letters of transit, but accepts the letters and hides them in the piano played by his house musician, Sam. Signor Ferrari, owner of the rival bar, the Blue Parrot, offers to buy Rick's, but Rick says his bar isn't for sale. Yvonne, Rick's neglected lover, tries to arrange a date for that evening, but Rick dismisses her attempts to pin him down and calls a cab to send her home. Louis tells Rick he's going to make an arrest and warns Rick against protecting the couriers' killer. "I stick my neck out for nobody," Rick responds. Louis also mentions Victor Laszlo, a famous Czech nationalist, will be arriving in Casablanca and warns Rick against trying to assist Laszlo, whose political activities are a threat to Nazi Germany. In the conversation that ensues, we learn that Rick was involved in anti-fascist wars in the 1930s, supporting the republicans in the Spanish Civil War and the Ethiopians against Italy. Strasser's arrival cuts the conversation short. With the German officer present, Louis decides the time has come to arrest the murderer of the German couriers. Cornered, Ugarte appeals to Rick for help, but Rick does nothing.

After things calm down, Laszlo enters the bar with Ilsa. They have come in search of exit visas. Strasser approaches Laszlo with hostility, and Louis politely requests that Laszlo report to his office the next day. Meanwhile, Ilsa and Sam, who recognize each other, begin to talk. At first Sam refuses Ilsa's request that he play "As Time Goes By," but eventually he relents. The music brings Rick to the piano. "I thought I told you never to play—" he begins, breaking off when he sees Ilsa. Laszlo and Louis join the group and Rick sits down for a drink, violating his rule of never drinking with customers. When the couple leaves, Rick picks up the check, breaking another personal rule.

Later that evening, Rick drinks alone after the bar closes. Sam plays "As Time Goes By" again, and Rick thinks about the past. In a flashback, we see a happier, less haggard Rick in Paris with Ilsa by his side. They drive through the city, ride a boat down the Seine, pop open a bottle of champagne, and dance at a club. Although they are clearly in love, they avoid all questions about each other’s pasts. When they hear word that the German army is approaching Paris, Rick knows he will have to leave the city. Rick proposes to Ilsa, who tries her best to hide her anguish, saying she doesn't plan that far in advance. Rick isn't worried, however. He thinks they'll leave together the next day for Marseille. At the train station the next day, Rick waits in the pouring rain, but Ilsa is nowhere to be found. Sam arrives and shows Rick a note. The note is from Ilsa and says that she can never see him again.

At this point, the flashback ends, and the scene shifts back to Casablanca. Ilsa enters the bar. She wants to explain what happened in Paris, but Rick is drunk and angry, and Ilsa leaves.

The next day, Ilsa and Laszlo go to Louis's office, where Strasser tells Laszlo he will never escape Casablanca alive. The couple then goes to the Blue Parrot to visit Signor Ferrari and arrive just as Rick is leaving. As Laszlo talks with Ferrari, Ilsa tells Rick that Laszlo is her husband and has been for years, even when she and Rick were together in Paris. Ferrari says he can obtain a visa for Ilsa but not for Laszlo. The couple decides not to split up. Ferrari suggests they speak to Rick, whom he suspects is holding Ugarte's stolen letters of transit.

That evening at Rick's, a young Bulgarian woman, Mrs. Brandel, approaches Rick to ask if Louis is "trustworthy." Louis, as Rick knows, has a habit of offering female refugees exit visas in exchange for sex. Rick says Louis's word is good, but, not wanting to let Mrs. Brandel's new marriage be harmed, he arranges for her husband to win big at the roulette table so they can buy a visa on their own. Later, Laszlo asks to speak to Rick. He offers to buy the letters of transit, but Rick says he'll never sell them. When Laszlo asks why, Rick replies, "Ask your wife."

German soldiers have gathered around Sam's piano and are singing the German national anthem. Laszlo tells the band to play "La Marseillaise," the French national anthem, and leads the patrons of the bar in a stirring rendition of the song, which drowns out the Germans. Strasser is furious and demands that Louis shut down Rick's. Louis closes Rick's on the pretext that gambling takes place there, even as he accepts his evening's winnings.

Back at their hotel, Laszlo asks Ilsa if there is anything she wants to tell him about Paris, and she replies “No.” Husband and wife reaffirm their love for each other. As Victor leaves for a meeting of the underground resistance, Ilsa leaves too and surprises Rick by showing up in his apartment. She pleads with him for the letters of transit, urging him to put aside his personal feelings for something more important. When Rick still refuses, she pulls a gun on him. Rick dares her to shoot, but Ilsa cannot. Instead, she breaks down in tears and claims she still loves Rick. They embrace.

Later, Ilsa explains what happened in Paris. After she married Laszlo, he had to return to Prague, where he was arrested and put in a concentration camp. Months later, she heard he was killed in an escape attempt. She met Rick shortly thereafter. Ilsa learned Laszlo was still alive just when she and Rick were about to leave Paris together. Laszlo needed her, and she decided to stay with him. She didn't tell Rick because she knew he wouldn't leave Paris if he found out, and then the Gestapo would arrest him. Her story finished, Ilsa says she'll never be able to leave Rick a second time, but she begs that he help Laszlo escape. As for whether she will go with Laszlo or stay with Rick, she says she doesn't know what's right anymore and tells Rick, "You have to think for both of us."

Later that evening, Laszlo claims to know that Rick loves Ilsa and asks for the letters of transit for her sake. Just then the German soldiers burst into Rick's and arrest Laszlo. The next day, Rick pleads with Louis to release Laszlo, saying that Laszlo can be arrested on a more serious charge, possession of the stolen letters of transit. Rick also tells Louis that he plans to use the letters to escape with Ilsa. That afternoon, Rick sells his club to Ferrari. That evening, back at Rick's, as he hands the letters to Laszlo, Louis emerges from the shadows to make the arrest. Just as suddenly, Rick pulls out a gun and points it at Louis. He orders Louis to call the airport to make sure that evening's flight to Lisbon will take off as planned. Louis pretends to call the airport, but actually calls Strasser.

At the airport, Rick makes Louis fill in the letters of transit for Mr. and Mrs. Victor Laszlo. Ilsa objects, but Rick says he did the thinking for both of them and decided that for Laszlo to continue doing his work, he needs Ilsa by his side. Rick tells Laszlo that Ilsa visited him last night and pretended to still love him to get the letters. He knew she was lying, Rick says, because it was over a long time ago. As Rick is talking, Ilsa sobs in the background.

After goodbyes, the Laszlos board the plane. Just then Strasser arrives. He calls the radio tower to prevent the plane's taking off, but Rick shoots him, and the plane leaves. The French police arrive, and Louis, who has seen everything, orders, "Round up the usual suspects." As the cops drive off, Louis congratulates Rick on becoming a patriot and then becomes a patriot himself, symbolized by his throwing out a bottle of Vichy water (water produced in the unoccupied region of France that persecuted Jews and tried to win Nazi favor). Rick and Louis walk along the runway together. Louis says he can arrange for Rick to escape to Brazzaville, another French colony in Africa, and then announces he will go, too. The movie ends with Rick saying the famous final line, "Louis, I think this is the beginning of a beautiful friendship." "La Marseillaise" plays in the background.