It is important to distinguish between rank and position. Rank is a service distinction based on experience, ability, and degree of responsibility. Position is a job distinction based on an individual's immediate responsibility and training.
Authority is first given on the basis of position, regardless of rank. Thus, direct orders from the captain of the ship take precedence over orders from a higher-ranking officer on board. Only if the captain is unable to function or unavailable will the orders of the ranking officer take precedence. Of course, a higher-ranking officer may be authorized to relieve the captain of his command.
In the absence of an assigned officer to the command position, the highest-ranking officer has the authority and responsibility of command. If there are two officers of equal rank, the officer with the longest time in grade takes command.
The Table of Ranks is as follows:
Lieutenant, Junior Grade
Chief Warrant Officer
Master Chief Petty Officer
Senior Chief Petty Officer
Chief Petty Officer
Petty Officer, First Class
Petty Officer, Second Class
Enlisted, First Class
Enlisted, Second Class
There are three grades of admirals: Flag Admiral, Vice Admiral, and Rear Admiral. It is acceptable to call all admirals by the title of "Admiral", as the difference in the rank is not that great. It is the positions they hold that differentiate them.
In TOS, all branch department head positions are held by Flag Admirals, who usually came up from Command branch. Major departments at this level include: Fleet Operations, Base Operations, Logistics, Personnel, Security, Science, Engineering, Medical, Research, Colonization, Justice, Exploration, and StarFleet Academy. The departments are sometimes also known as "Corps" or "Command". Examples are StarFleet Engineering Corps and StarFleet Exploration Command.
Insignia: four stars
Flag Admirals are the ones making the strategic decisions for StarFleet. There are actually two types of Flag Admirals. The ones commanding numbered fleets (i.e. Seventh Fleet - Romulan Border) are known as Fleet Admirals. Those who command a major department (logistics, personnel, etc.) are known as Branch Admirals. Only personnel who came up through command can achieve this rank.
Insignia: three stars
Vice admirals are often involved in a variety of roles, ranging from diplomatic and scientific to traditional planning and organizational tasks. Diplomatic liaison and/or negotiations with new member planets would often require services of a vice admiral. The Star Fleet representative to the Federation is a Vice Admiral.
When commanding ships, vice admirals command special task groups under the major fleets of about two dozen ships or more.
Insignia: two stars
Commander of an operational fleet of approximately dozen ships, the Rear Admirals perform tasks of the admiralty that involves more of a "local" nature, limited to a more specific astrographical location, such as a sector, quadrant, etc. Often they are also the starbase commanders.
Insignia: one star
The Rank of Commodore is functionally equivalent to a Fleet Captain, but in this time period is accorded flag officer status -- marked by a star as admirals do. (Later (in the Next Generation period), commodores were not treated as flag officers and wore five pips on the collar like senior captains.) A Commodore may assume temporary operational command of a squadron or fleet, but is not in permanent command.
Commisioned officers are responsible for directly running the starships and starbases of the Federation. They include captain, commander, lieutenant commander, lieutenant, lieutenant junior grade (j.g.), and ensign. Commissioned Officers generally comprise ten percent (10%) of the crew on a starship.
Insignia: three stripes
A Fleet Captain is the senior captain of a fleet under command of a Rear Admiral. The Rear Admiral may delegate the authority to the Fleet Captain, who will actually run the fleet. Later, the rank evolved to a more administrative position.
For example, Garth of Izar is of that rank due to his importance in the Battle of Axanar. He was the only one to have ever defeated a Klingon fleet in battle at that time. The Commodore who reinforced him decided to delegate the overall command to Garth, promoting him to Fleet Captain. The only other fleet captain known is Fleet Captain Christopher Pike.
Insignia: Two and a half stripes
The usual rank for the commanding officer of a major ship (a cruiser or larger). The term "captain" is also sometimes used to mean the position of commanding officer of a ship, regardless of official rank. Captains may also be senior Star Fleet ambassadorial attache.
Insignia: Two stripes
The executive officer of a major ship (which has a "Captain" as captain) is probably a commander. Some smaller ships (frigates and destroyers) are commanded by Commanders and they are known as "brevet captains", who are usually addressed as "skipper" (instead of "captain") while onboard. Since cruises on small ships are used to train the Commanders for promotion, it is possible for a full Captain (albeit a young one, recently promoted) to command a small ship.
The Chief Medical Officer of a heavy cruiser or above holds the rank of full commander.
During a crisis, with the captain incapacitated, the executive officer can take over a cruiser as "brevet captain". If the captain cannot be returned to duty, the promotion will be made permanent after the crisis is resolved.
Insignia: One and a half stripe
An officer with rank of Lieutenant Commander could be commander of a frigate or a police corvette, or executive officer of a ship with a Commander as the skipper. A third possibility is s/he was given the post of executive officer on a major ship (heavy cruiser or above) and will be promoted to full Commander if s/he can handle the job. Spock would be such a case, since he was a lieutenant commander, yet wearing insignia of a full commander.
On starships, the LtCmdr's are usually the department heads (engineering, medical, navigation, etc.). Senior department head onboard is probably also the second officer, third in chain of command after Captain and the exec.
Insignia: One stripe
Full lieutenants are the junior officers with the most skill and seniority. They are also the deputy department heads on cruiser or larger vessels, and department heads on smaller ships.
Lieutenants may be in charge of a very small ship, like a freighter, in special situations. Lieutenants are also commanders of fighter-shuttle squadrons at times of war.
Insignia: half stripe
Also known as "jay-gee's", these are the junior lieutenants, usually on their second or third cruise. They might be in charge of a very small ship under very special situations. Otherwise they are the ones who get their hands dirty in ship's departments. They also could be deputy department heads in the minor departments.
Insignia: No stripe
Ensigns are the "working" officers on their first cruise. Because an officer is an officer, ensigns are sometimes assigned to command small units or to perform certain tasks that cannot be delegated to petty officers. They can hold bridge duty, but only the most senior do so during primary (day) shift.
Insignia: No stripe
Cadets are neither officers nor enlisted personnel, but officers in training, therefore they are included under commissioned officers, even though they have not yet received their commissions. As students of StarFleet Academy, they might be on a ship for a training cruise (as seen in The Wrath of Khan). Cadets have no insignia, and wear special uniforms that designate their officer-in-training status.
Salutes are never required of ranking officers, instead being a courtesy. You are to respect superior officers, and stand at attention. Formally, a higher-ranking officer is always to be addressed as "Sir". The pronoun "Maam" is not used unless so ordered.
Less formally, individuals are most commonly referred to by their rank name or position. A Medical Officer may be referred to as "Doctor". A Security Chief and some other Staff officers may be referred to as "Chief".
Lower-ranking officers may be referred to as "Mister", or by their rank name. The terms "Miss" and "Ms. " are never used.