Jamaica Inn

Jamaica Inn is the headquarters of a gang of wreckers led by the innkeeper Joss Merlyn (Leslie Banks). The wreckers are responsible for a series of engineered shipwrecks in which they extinguish coastal warning beacons to cause ships to run aground on the rocky Cornish coast. Then they kill the surviving sailors and steal the cargo from the wrecks.
Beautiful, young Mary Yellan (Maureen O'Hara) is travelling to Jamaica Inn in a carriage, but the driver is afraid to stop there and drives a long way past the inn in spite of her repeated demands that he stop. Instead, he drops her off near the home of the local squire and justice of the peace, Sir Humphrey Pengallan (Charles Laughton). She meets him and requests the loan of a horse so she can go back to Jamaica Inn. Pengallan warns her not to go there. Nevertheless, Mary says she just came from Ireland, and as the orphaned niece of Joss's wife Patience (Marie Ney), she intends to live at Jamaica Inn. Unable to convince her to stay, Pengallan accompanies Mary to Jamaica Inn.
When Joss meets Mary, he tries to kiss her and push himself on her. Patience is distraught to learn her sister, Mary's mother, has died. When Mary tells Joss that Pengallan has accompanied her to Jamaica Inn, Joss goes upstairs and reports to Pengallan. We learn that Pengallan is the secret criminal mastermind behind the wrecking gang, who finds out when well-laden ships are passing near the coast, determines when and where the wrecks are to be done, disposes of the stolen cargo, and uses the booty to support his lavish lifestyle, passing a small fraction of the takings back to Joss and the gang.
In another part of the inn, the gang convenes to discuss why they get so little money for their efforts. They suspect Jem Traherne (Robert Newton), a gang member who has been with them for only two months, of embezzling goods. They hang him from one of the rafters of the inn, but Mary cuts the rope afterwards and saves his life. Together, Traherne and Mary flee from the gang. They are almost recaptured and have to swim for their lives in order to escape.
The next morning, they seek the protection of Pengallan, unaware that Pengallan is the secret leader of Joss's gang. Traherne reveals to Pengallan that he is actually an undercover law-officer on a mission to investigate the wrecks. Pengallan is alarmed, but he maintains his composure and pretends to join forces with Traherne. Mary overhears this conversation and goes to the inn to warn Patience that law enforcement knows about the wrecking gang and that she must flee in order to avoid being arrested as an accomplice. However, Patience refuses to leave her husband. Traherne and Pengallan also go to the Inn to search for evidence. While Traherne is searching the Inn, Joss and the gang return. Pengallan takes Joss aside and tells him a wreck must be performed immediately because he needs the money to escape to France now that the gang's activity has come to the attention of law enforcement. He advises Joss to go into hiding as well as soon as the wreck is complete. Joss and the gang go to do the wreck, leaving Traherne tied up. Joss also pretends to tie up Pengallan as a ploy to fool Traherne, but he secretly leaves Pengallan's hands free.
After the gang leaves, Pengallan releases himself from his bonds and reveals himself to Traherne. He gives Patience a loaded pistol and instructs her to shoot Traherne if he gets loose. Pengallen then leaves. Traherne reasons with Patience, and promises to let her and Joss escape if she releases him. She agrees and unties him. He sets out for a nearby military camp to get backup. Meanwhile, Mary goes to the wrecking site, and re-lights the warning beacon which the wreckers had extinguished. The crew of the ship see the beacon and turn away; the ship does not wreck. The gang capture Mary and resolve to kill her for preventing the wreck. Joss, who has developed a reluctant admiration for her spirit even though she has thwarted his plans twice, rescues her from the gang and the two escape by horse-cart, but Joss is shot in the back while escaping and collapses when they reach Jamaica Inn. As Patience is about to tell Mary that Pengallan is the secret leader of the wrecking gang, Pengallan shoots and kills Patience from offstage. Joss then dies of his wound as well. While Mary is reeling from the shock of witnessing these two violent deaths, Pengallan reveals himself to her, takes her hostage, ties her hands behind her back, gags her, and tells her that he plans to keep her and take care of her now that she has no one else in the world. He drives her, still tied up and covered by a heavy cloak, to the harbor and they board a ship bound for France.
The gang returns to Jamaica Inn to find Joss and Patience dead. Just then, Traherne arrives with a posse of soldiers, who take the gang into custody. Traherne goes to the harbor with some of his soldiers to rescue Mary, and Pengallan is cornered on the ship. He climbs to the top of a mast, where he clumsily drops his gun. As the soldiers climb up after him, he jumps to his death, shouting "Make way for Pengallan!" Traherne leads Mary away from the scene, and Pengallan's horrified butler (Horace Hodges) is left wondering to himself, with a memory of Pengallan's imperious voice ringing in his ears.

Question: Who rescues mary from the gang and why?