A commanding officer (or CO) is a term used to describe one's rank in relation to others within the same environment.
One meaning of commanding officer is the highest-ranking officer and commander of a unit. On starships, the CO usually has the rank of captain, though sometimes a commodore or fleet captain may also hold this position. On starbases, the CO is sometimes referred to as "portmaster" and was usually a flag officer. (TOS: "Court Martial", "The Menagerie, Part I", "The Menagerie, Part II", "The Deadly Years") Some bases, however, are commanded by captains or commanders. (Star Trek: Deep Space Nine, TNG: "11001001")
It is also possible for lower ranks to be the commanding officer aboard a starship, such as the case with Lieutenant Commander Tom Markel aboard the USS Ariel. Other examples include Starfleet Command temporarily assigning a number of officers to command vessels that blockaded the Romulan support of the House of Duras in 2368. Lieutenant Commander Piersall commanded the USS Prometheus in 2370. (DS9: "Second Sight") Major Kira Nerys and Lieutenant Commanders Jadzia Dax and Worf commanded the USS Defiant on a number of occasions. In 2373, Major Kira commanded the Defiant to destroy a Klingon minefield near the Bajoran system. Commander Worf assisted Captain Sisko in 2372 by bringing the Defiant to Earth to help quash Admiral Leyton's attempted coup d'état. Later, Worf helped is former commanding officer, Captain Jean-Luc Picard, in the Battle of Sector 001. Commander Dax commanded the Defiant in 2374 while operating out of Starbase 375. Her mission was to destroy a Dominion sensor array hidden in the Argolis Cluster. (TAS: "The Eye of the Beholder"; TNG: "Redemption II"; DS9: "Second Sight", "Sons of Mogh", "Paradise Lost"; Star Trek: First Contact; DS9: "Behind the Lines")
On starships, it is general practice to address the commanding officer as "Captain," regardless of their actual rank or position. (DS9: "Behind the Lines")
In general, commanding officers are dressed in the colors of the command division, but there are some exceptions in Starfleet uniform usage.
In rare occasions, the commanding officer need not be a ranking officer, especially in dire circumstances. (DS9: "Valiant")
Dukat once claimed it was Cardassian tradition for the commanding officer to entertain his guests. (DS9: "Return to Grace")  Chain of command
The term "commanding officer" can also be used to refer to the officer whom one directly reports to in the chain of command or in a department. Occasionally, it simply identifies an officer of higher rank in comparison to another, or the officer with the most experience if more than one officer holds the highest rank at the time, although this is usually referred to as a ranking officer.
Lieutenant Commander Geordi La Forge once referred to himself as the commanding officer of Lieutenant Reginald Barclay. (TNG: "Hollow Pursuits")
Commander William T. Riker was adamant about following the orders of one's commanding officer when confronting his transporter duplicate Thomas Riker. (TNG: "Second Chances")
The Romulan defector "Setal" admitted to William Riker that his commanding officer was Admiral Alidar Jarok. (TNG: "The Defector")
Worf acknowledged the fact that Riker was his commanding officer before the latter agreed to the former joining him on an "unannounced visit" to the J'naii planet. (TNG: "The Outcast")
Riker again brought up the issue of one's commanding officer when he, under threats by Arctus Baran, demanded that Data withdraw the USS Enterprise-D from their attempt to stop Baran's mercenary ship. (TNG: "Gambit, Part I")