A very good question! The story is based on a historical incident, but because of the first-person point of view, we know only what the captain tells us. Most critics agree that Leggatt is real. Taking Leggatt onboard and hiding him is against maritime law, but doing so successfully gives the captain a sense of accomplishment that reaches its peak when he manages to deposit Leggatt as close to shore as possible without running his ship aground, thereby impressing his crew, who previously doubted his abilities as a captain. The entire experience serves as a kind of initiation for the young captain.
Others, however, contend that because no one else sees Leggatt, he doesn't actually exist. If that theory is true, then why does the captain "imagine" taking Leggatt onboard before Captain Archbold comes in search of him? How would the captain even know about Leggatt?
It's understandable that the captain needs an ally of sorts on a ship where the crew has been together for months and he is the new leader. To meet his "double," his "secret sharer," gives him the opportunity to explore parts of his personality he had never considered before. He can determine the kind of captain he wants to be because he learns of Leggatt's experience.