The novel is set during May and June 1914; war was evident in Europe, Richard Hannay the protagonist and narrator, an expatriate Scot, returns to his new home, a flat in London, after a long stay in Rhodesia, in order to begin a new life. One night he is buttonholed by a stranger, a well-travelled American, who claims to be in fear for his life. The man appears to know of an anarchist plot to destabilise Europe, beginning with a plan to assassinate the Greek Premier, Constantine Karolides, during his forthcoming visit to London. The man reveals his name to be Franklin P. Scudder, a freelance spy, and remarks that he is dead, which holds Hannay's attention. Scudder explains that he has faked his own death in order to avert suspicion. Scudder claims to be following a ring of German spies called the Black Stone who are trying to steal British plans for the outbreak of war. Hannay lets Scudder hide in his flat, and sure enough the next day another man is discovered having apparently committed suicide in the same building. Four days later Hannay returns home to find Scudder dead with a knife through his heart.
Hannay fears that the murderers will come for him next, but cannot ask the police for help because he is the most likely suspect for the murders because he lived in the same building. Not only does he want to avoid imprisonment, but he also feels a duty to take up Scudder's cause and save Karolides from the assassination, planned in three weeks' time. He decides to go into hiding in Scotland and then to contact the authorities at the last minute. In order to escape from his flat unseen, he bribes the milkman into lending him his uniform and exits wearing it, escaping from the German spies watching the house. Carrying Scudder's pocket-book, he catches an express train leaving from London St. Pancras station. Hannay fixes upon Galloway, in south-west Scotland, as a suitably remote place in which to make his escape and remembers somehow the town of Newton-Stewart, which he names as his destination when he buys his ticket from the guard.
Arriving at a remote station somewhere in Galloway (apparently not Newton Stewart itself), Hannay lodges in a shepherd's cottage. The next morning he reads in a newspaper that the police are looking for him in Scotland. Reasoning that the police would expect him to head for a port on the West Coast, he doubles back and boards a local train heading east, but jumps off between stations. He is seen but escapes, finding an inn where he stays the night. He tells the innkeeper a modified version of his story, and the man is persuaded to shelter him. While staying at the inn, Hannay cracks the substitution cipher used in Scudder's pocket-sized book. The next day two men arrive at the inn looking for Hannay, but the innkeeper sends them away. When they return later, Hannay steals their car and escapes.
On his way, Hannay reflects on what he has learnt from Scudder's notes. They contradict the story that Scudder first told to him, and mention an enemy group called the Black Stone and the mysterious Thirty-nine Steps . The United Kingdom appears to be in danger of an invasion by Germany and its allies. By this time, Hannay is being pursued by an aeroplane, and a policeman in a remote village has tried to stop him. Trying to avoid an oncoming car, Hannay crashes his own, but the other driver offers to take him home. The man is Sir Harry, a local landowner and prospective politician, although politically very naïve. When he learns of Hannay's experiences in South Africa, he invites him to address an election meeting that afternoon. Hannay's speech impresses Sir Harry, and Hannay feels able to trust him with his story. Sir Harry writes an introductory letter about Hannay to a relation in the Foreign Office.
Hannay leaves Sir Harry and tries to hide in the countryside, but is spotted by the aeroplane. Soon he spots a group of men on the ground searching for him. Miraculously, he meets a road mender out on the moor, and swaps places with him, sending the workman home. His disguise fools his pursuers, who pass him by. On the same road he meets, in a passing touring car, a Society sycophant whom he recognizes from London and whom he forces to exchange clothes with him and drive him off the moor.
The next day, Hannay manages to stay ahead of the pursuers, and hides in a cottage occupied by an elderly man. Unfortunately, the man turns out to be one of the enemy, and with his accomplices he locks Hannay into his storage room. Fortunately, the room in which Hannay is locked is full of bomb-making materials, which he uses to break out of the cottage, injuring himself in the process.
A day later, Hannay retrieves his possessions from the helpful road mender and stays for a few days to recover from the explosion. He dines at a Public House in Moffat before walking to the junction at Beattock to catch a southbound train to England, changing at Crewe, Birmingham New Street and Reading, to meet Sir Harry's relative at the Foreign Office, Sir Walter Bullivant, at his country home in Berkshire. As they discuss Scudder's notes, Sir Walter receives a phone call to tell him that Karolides has been assassinated.
Sir Walter, now at his house in London, lets Hannay in on some military secrets before releasing him to go home. Hannay is unable to shake off his sense of involvement in important events, and returns to Sir Walter's house where a high-level meeting is in progress. As one of the men, ostensibly the First Sea Lord, leaves the meeting, Hannay recognizes him as one of his former pursuers in Scotland. Hannay warns Sir Walter and the other officials that the man was an impostor, and they realise the spy is about to return to Europe with the information he has obtained from their meeting. At that point, Hannay realises that the phrase "the thirty-nine steps" could refer to the landing-point in England from which the spy is about to set sail. Throughout the night, Hannay and the British military leaders try to work out the meaning of the mysterious phrase.
After some reasoning worthy of Sherlock Holmes, and with the help of a knowledgeable coastguard, the group decide on a coastal town in Kent. They find a path down from the cliff that has thirty-nine steps. Just offshore they see a yacht. Posing as fishermen, some of the party visit the yacht, the Ariadne , and find that at least one of the crew appears to be German. The only people onshore are playing tennis by a villa and appear to be English, but they match Scudder's description of the conspirators, The Black Stone. Hannay, alone, confronts the men at the villa. After a struggle, two of the men are captured while the third flees to the yacht, which meanwhile has been seized by the British authorities. The plot is thwarted, and the United Kingdom enters the First World War, having kept its military secrets from the enemy.
On the outbreak of war, Hannay joins the New Army and is immediately commissioned captain.