The story takes place on a sailing ship in the Gulf of Siam (now the Gulf of Thailand), at the start of a voyage with cargo for Britain. The date is probably in the 1880s (when Conrad was at sea himself). In common with many of Conrad's stories, it is narrated in the first person. The narrator is the ship's young captain, and he is unfamiliar with both his ship and his crew, having joined the ship only a fortnight earlier. He is unsure of his ability to exert his authority over the officers and crew who have been together for some time, and makes the point several times that he is the "stranger" on board.
After being towed down-river (presumably from Bangkok) by a steam tug, the ship is left at anchor near a group of small barren islands a few miles off shore, waiting for wind to begin its voyage. An incoming ship is anchored similarly a couple of miles away, awaiting a tug to go upriver.
That night, the captain, being restless, unusually takes the watch. As the only man on deck in the small hours, he sees that a man has swum up to the ship's side. The naked swimmer is hesitant to talk or come on board, but seems pleased to discover he is speaking to the captain. Once on board he and the captain find a natural rapport, almost as if he, Leggatt, were the captain's other self; especially as the captain has now fetched some of his own clothes that Leggatt is now wearing.
Still on deck, Leggatt explains that he was the First Mate of the other ship, but under arrest for murdering a crew member. The victim was a disobedient bully. During a storm which nearly sank their ship on their voyage here, Leggatt was physically wrestling with the man to make him to pull a rope when a freak wave threw them both against a bulwark and the man was killed. Leggatt, a "stranger" on the other ship just as our captain was on this, would certainly face the gallows on landing. However, he had escaped his locked cabin and had swum between islands to reach the narrator's ship.
This is the point at which our captain could, and by all the rules should, arrest Leggatt. But instead he leads him to concealment in his cabin. The captain has no plan yet, and the situation is one of extreme difficulty, with his cabin regularly serviced by his steward, the problem of food, a ship's captain's movements being conspicuous to all, and a long voyage ahead. In the morning the captain of Leggatt's ship arrives by boat to enquire if the escapee has been sighted. Our captain, not a natural liar, manages to bluff through, but is left terrified as to what his own officers make of his strained behaviour.
With rising wind the ship gets under way, and there starts a routine of the captain helping Leggatt evade the dutiful visits of the steward to the captain's cabin. Leggatt comes close to discovery several times, almost like a stage farce. All the while, the captain is tormented by any small sign that any of his crew suspect (or even might have discovered) the secret. The captain and Leggatt evolve a plan; Leggatt, being a good swimmer, will drop into the sea and swim ashore further down the Gulf of Siam while the ship is sailed as close in to land as possible. This is done, although the risky manoeuver under the captain's direct command nearly puts the ship onto the rocks, testing his seamanship and horrifying the crew. He succeeds, and leads the ship away.

Question: Why do the Captain and Leggatt get along so well?
Answer: They were both "strangers" on their ships