For a ship there are actually two considerations, on a US Naval vessel they are addressed by Departmental/Divisional organizations and Duty Sections. While I know that this is from a US Navy perspective, all ships have something similar. My background is as an Airdale, so I will use mostly squadron examples since I am most familiar with them.
The first consideration are the professional skills of jobs. These are usually broken down within Departments. The Engineering Department for example. The full layout is usually modified by what the unit does for instance a Naval Squadron has two Departments, Operations and Maintenance. STA breaks them into three, Command, Operations and Sciences. Yes, they called them “Divisions” but that is really semantics. Each Department is broken down into specialties (Divisions) which in turn is broken into Branches if needed.
For example, the Maintenance Department has six Divisions; Maintenance/Material Control, Quality Assurance, Maintenance Administration, Aircraft, Av/Arm and Line.
Av/Arm Division is broken down into three branches; Avionics, Electricians and Ordnance.
The Department/Division structure addresses the need to identify and organize the professional skill sets needed to accomplish the tasking such as operating the ship or aircraft. Exactly what jobs and skills are required.
The same need will be fulfilled by a ship in Star Trek though they will use different terminology.
The second consideration is how many? A ship operates 24 hours a day 7 days a week, even in port or the ship yard. It requires constant attention and upkeep and lack if either can result in material damage at best and casualties (human or equipment) at worst.
The standard in Naval Aviation is to have 4 Duty Sections divided into the Port and Starboard Watch. Section 1 and 3 are Port and 2 and 4 are Starboard. The Surface Navy also uses Sections and Watches that can have a slightly different setup due to circumstances and task. For instance, a job that requires a constant and high level of alertness but consists of tasking that promote drowsiness such as staring at gauges may have a shorter duration. The watch could be 2, 4, 6, 8 or 12 hours rotating on and off time to maintain alertness.
But over all each Duty Section contains all the personnel and skill sets needed to operate and fight the ship, or what have you. In the aviation community we generally operated on three eight hours shifts or two twelve hours shifts. The Port Watch alternating 12 hour shifts can sustain operations for long periods of time. All four sections can sustain 24 hours operations for months without a break and still absorb injuries and sickness without totally hammering the personnel. In dangerous situations such as combat having four sections gives the unit the depth needed to support damage control, repair parties as well as combat losses and still fight the ship.
I use the aircraft carrier as a guide for the Trek ships because you can loosely match them up.
Command – In addition to ships personnel, the carrier usually has an Admiral and his operations/intelligence staff commanding the Battlegroup.
Operations – These guys run and fight the ship itself, engineering, deck etc.
Sciences – I liken this to the Airwing and AIMD. No, Treks Sciences don’t fly small attack craft, but they are a department on the ship that operates completely separate from the ship itself. Their purpose is life is directly supported by the rest of the ships crew.
Command will be the smallest department numbers wise.
Operations will be the largest as they are tasked with maintaining and operating the ship and all its equipment.
Sciences will fluctuate depending on what they are studying/exploring and the ships purpose. A science or exploration ship will have far more science types than a full-on warship.
So crew size depends on the mission/type of ship and what professional skill sets are needed multiplied by the degree of redundancy needed to sustain operations over time. For the units I have served with two per job per shift was absolute minimum. For some branches two qualified per Duty Section was the minimum with additional personnel in various states of training. Having at least two people working a gripe can be mandatory just for safety. I never let an Electrician go in work alone, no one should ever work around high voltage by themselves.