I'm pretty sure "Commander Shepard" is preferable to "commander Shepard," but I'm less sure about "the Commander" vs "the commander." On one hand, I'm pretty sure "commander" is a common noun in this usage, and I certainly wouldn't write "a Commander." But on the other, capitalizing it in this instance feels righter to my native ear. I'm starting to think this is like the serial comma (i.e. pick a style and be consistent), but if there is a real rule, I'd like to follow it.
This is a style issue, and the appropriate style for you to use depends on which style guide you or your publisher or school generally follows and whether that guide specifies a rule for handling military (or other hierarchical) titles. The influential (in the United States) Chicago Manual of Style, fifteenth edition (2003), lays out one approach:
Titles and Offices
8.21 Capitalization: the general rule/ Civil, military, religious and professional titles are capitalized when they immediately precede a personal name and are thus used as part of the name (usually replacing the title holder's first name. Titles are normally lowercased when following a name or used in place of a name ...
[Examples:] President Lincoln; the president; General Bradley; the general; Cardinal Newman; the cardinal Dean Mueller; the dean; Governors Edgar and Ryan; the governors
Exceptions exist, according to Chicago, but these primarily involve instances of direct address (Yes sir, Captain!") or formal introduction ("Ladies and Gentlemen, the Prime Minister"), as well as various special forms of displayed lists or promotional presentations.
In a normal situation in which you were referring to a particular commander as "the commander" (or "the Commander"), Chicago advises using "the commander."
The Oxford Guide to Style (2003) agrees with Chicago on capitalization of titles used immediately before a name and noncapitalization of titles identified immediately after a name:
4.1.7 Titles of rank or relationship, and nicknames:
Titles used as identification or clarification after a name normally are not capitalized (especially in US usage):
[Examples:] Miss Dunn, the head teacher; Anne Williams, our mnaging director; Mr. Gladstone, the prime minister; Dr Primrose, the parish vicar
Titles used before a name are normally capitalized, and are not followed by a comma:
[Examples:] Head Teacher Miss Dunn; Managing Director Anne Williams; the Prime Minister Mr. Gladstone; The Reverend Dr Primrose
But Oxford breaks with Chicago on the very point that the OP asks about:
Capitals are preferred, however, when a short-form mention of a title i used as synonym for a particular person, an organization in an institutional or official sense, or a government:
[Relevant examples:] the Duke; the Princess, the Ministry (of Defence); the Centre's policy
So if you already introduced Commander Shepard earlier in a piece of writing, and you now wish to refer to the same person as "the Commander," Oxford advises you to capitalize the C in Commander.
In the United States, I believe, the Chicago style is more prevalent than the Oxford style. It is certainly the style we followed at the tech magazines where I've worked. But in the UK, the Oxford style may be dominant.
All titles are capitalized. Verbs aren't.
When using captain in sentences, capitalize it when referring to an individual, even if not by name. As @Oldcat suggested, you would capitalize in a sentence starting with "The Captain..." because you're referring to one person, who has the title of Captain. But when you're using the word to refer to captains non-personally, as a verb, i.e. "Sometimes captains are..." or "All ships are captained..." I wouldn't capitalize it.