In the opening scene of this episode, they state that crewman Norman has been recently assigned to the ship. So presumably they picked him up from a Starfleet base somewhere, where... well, I dunno. Maybe he'd been posing as a Starfleet lieutenant ever since... he'd signed up four years ago and been through basic training and served on some other ships to get the rank of lieutenant. Whatever he did, it's some seriously deep undercover work to get on board Enterprise as an impostor Starfleet officer.
Also, either Norman is some sort of computer hacking über-genius, or it's entirely possible for somebody to type two commands into a random terminal anywhere on the ship and make the helm at Sulu's bridge station completely unresponsive to anything the helmsman can do. That's some security system on that ship.
And all that happens before the opening panel of this strip!
Captain's log, Stardate 4513.3. Crewman Norman has hijacked the Enterprise.
Sulu: Helm unresponsive! Warp 7.
Norman: I am an android. I will now switch myself off.
Caption: How did he even get on board?
Spock: If we touch anything, the ship will explode.
Kirk: Well, let's do absolutely nothing to get out of this situation for 4 days, then.
Harry Mudd: Welcome to my planet, Kirk!
Kirk: Harry Mudd! Weren't you in prison?
Harry Mudd: Long story. Let me fill 5 minutes of screen time telling it.
Harry Mudd: ... And here I am! On a planet of androids loyal to my every wish! Correct?
Alice 1: Yes, Lord Muck.
Harry Mudd: Mudd! Lord Mudd!
Harry Mudd: I had them make an exact replica of my nagging, annoying wife.
Kirk: Why would you do that? And why show us?
Chekov: Captain, something just happened to my gun...
Stella: Harcourt Fenton Mudd!
Harry Mudd: I'll strand your entire crew here, and depart on your ship.
Norman: And we will conquer all humans in the Galaxy.
Harry Mudd: Wait, what?
Spock: We need to find the android control centre.
Kirk: How in blazes do we do that, Spock?
Norman: This android control centre is impervious to your machinations.
McCoy: Well, now what?
Kirk: Androids are computers, right? I'll talk them into a logical paradox!
Spock: That has only worked three times for us before.
Kirk: You're right. We need a better plan.
Kirk: We'll destroy them with logical paradoxes and hammy acting!
Spock: A most potent combination.
Harry Mudd: I'll help. My acting is almost as hammy as yours, Kirk.
Chekov: We are sad!
Uhura: And yet we are acting happy!
Alice 118: Illogical!
Alice 2: Illogical!
SFX: bzzt! bzzzt!
Kirk: Harry Mudd is a liar. Everything he says is a lie.
Harry Mudd: I am lying right now!
Spock: We must take Mudd to justice.
Kirk: No, let's strand him on this planet with 500 copies of his nagging wife.
Spock: Have you read Starfleet regulations?
Stella1 and Stella2: Harcourt Fenton Mudd!