Having deduced that red = engineering and blue = medical/science, I guess mustard is... line officer?
You wouldn't see any information to the contrary until February 1967.
Specifically "line officer" in the naval sense (as opposed to "staff officer"). Which, since that's defined as persons "trained and qualified to exercise military command", describing mustard as "command" makes sense, too. I guess that answers my question!
I find it interesting that, in "The Menagerie", Spock surrenders himself to McCoy as he's the senior ranking officer, but McCoy, as Medical Officer, is staff, not line. Is that how it would work in the contemporary wet navy, or would Spock surrender to Hansen (presumably the senior line officer at the time, though not necessarily -- he might simply have been OOD).
A couple of different things. A "staff officer" generally speaking is one who works on the staff of a senior officer, as opposed to commanding people in an operational unit. Officers rotate between operational and staff assignments (which are usually shore duty) over the course of their career. In this sense, a USN line officer can be a staff officer.
A "staff corps" officer is a USN thing. Staff corps officers are members of a specialized "corps," established by legislation. The staff corps are: Medical (physicians), Dental, Nurse, Medical Service (other medical specialties), Chaplain, Supply, Civil Engineer (Seabees) and JAG (lawyers). Extinct corps are Engineer (marine propulsion), Construction (naval architecture) and Professors of Mathematics. Through WW1, the staff corps (or "civil branches" in the Royal Navy) had their own specific officer titles, only in 1918 would they be called "lieutenant," "commander" etc.
A USN line officer was originally any officer who was qualified to take command, or succeed to command, of a vessel at sea. Now there are also "restricted line" officers, who can't take command at sea but also don't fall under any of the established staff corps. Examples are EDO (engineering duty only), HR, intelligence, oceanography, public relations, music. So sea-command officers are sometimes referred to as "unrestricted line."
Quartermasters, pursers, medical officers, chaplains -- those are explicitly staff officers in my 1944 Bluejacket's Manual. Gunnery and navigation had their own departments, and on Trek, their officers wear command colors. Would they have been line officers back in the day? What about engineers and damage control officers?
A quartermaster in the US Navy is a petty officer that specializes in navigation, steering and the like. A purser was the old name for a supply officer, early 1800s or before.
It depends how far you want to go back. Since 1899, when the Engineer Corps was abolished, a US Navy ship's engineering officers are line officers. The UK Royal Navy tried that in the early 1900s but in the '20s went back to a system where engineers were a separate branch and could not be in line to command a vessel. In 1955 they adopted a new system where "seaman," engineer and supply officers all wore the same insignia and were interchangeable for command on shore, but only seaman officers could command at sea.
For the USN the decks, hull and exterior were originally separate from the engineering department, and this came to include damage control. In WW2 this department was called the hull or C&R department (construction and repair) and was headed by an officer called, regardless of rank, the first lieutenant. In the '50s the damage control responsibility shifted to the engineering department.
As a rule, the only departments on a US Navy ship which are not headed by a line officer are supply and medical.
Do navy ships have their own armed guards, or do they usually employ marines for that duty?
It's all sailors, ships' marine detachments were eliminated about 25 years ago. And even then only the really big ships had them.