Jim (his surname is never disclosed), a young British seaman, becomes first mate on the Patna, a ship full of pilgrims travelling to Mecca for the hajj. When the ship starts rapidly taking on water and disaster seems imminent, Jim joins his captain and other crew members in abandoning the ship and its passengers. A few days later, they are picked up by a French ship. However, the Patna and its passengers are later also saved, and the reprehensible actions of the crew are exposed. The other participants evade the judicial court of inquiry, leaving Jim to face the court alone. The court strips him of his navigation command certificate for his dereliction of duty. Jim is angry with himself, both for his moment of weakness, and for missing an opportunity to be a 'hero'.
At the trial, he meets Charles Marlow, a sea captain, who in spite of his initial misgivings over what he sees as Jim's moral unsoundness, comes to befriend him, for he is "one of us". Marlow later finds Jim work as a ship chandler's clerk. Jim tries to remain incognito, but whenever the opprobrium of the Patna incident catches up with him, he abandons his place and moves further east.
Later, Marlow's friend Stein suggests placing Jim as his factor in Patusan, a remote inland settlement with a mixed Malay and Bugis population, where Jim's past can remain hidden. While living on the island he acquires the title 'Tuan' ('Lord'). Here, Jim wins the respect of the people and becomes their leader by relieving them from the predations of the bandit Sherif Ali and protecting them from the corrupt local Malay chief, Rajah Tunku Allang. Jim wins the love of Jewel, a woman of mixed race, and is "satisfied... nearly". The end comes a few years later, when the town is attacked by the marauder "Gentleman" Brown. Although Brown and his gang are driven off, Dain Waris, the son of the leader of the Bugis community, is slain. Jim returns to Doramin, the Bugis leader, and willingly takes a fatal bullet in the chest from him as retribution for the death of his son.
Marlow is also the narrator of three of Conrad's other works: Heart of Darkness, Youth, and Chance.

Question: Why was Jim executed?
Answer: in retribution for the death of Dain
Alternative answer: He sacrificed himself as retribution for the death of the Bugis leader's son.