Judge Advocate General

A Judge Advocate General (often shortened to JAG) within Starfleet was an officer who advised Starfleet on and administered courts martial, as well as overseeing other Starfleet legal matters and personnel. Legal counsel within Starfleet reported to the senior JAG officer as part of the department known as the Judge Advocate General's Office (or simply the "Judge Advocate's Office"). During the 2260s, the JAG Office was part of Starfleet's operations division. By the 2350s it had moved under the command division.

History
Areel Shaw was a Judge Advocate assigned to Starbase 11 in 2267. While there, she prosecuted the court martial of James T. Kirk on the charge of culpable negligence. 

Phillipa Louvois was also a member of the Judge Advocate General's Office. In 2355, she presided over an investigation into the loss of the USS Stargazer. In 2365, as a captain, she was the commanding officer of the JAG office of Sector 23 on Starbase 173. One of Captain Louvois' first rulings in 2365 was that Lieutenant Commander Data was not the property of Starfleet. 

The Judge Advocate General's Office was responsible for an investigation into a mutiny that occurred aboard the USS Pegasus in 2358. 

As Starfleet's Judge Advocate General in 2373, Rear Admiral ruled on Richard Bashir's criminal actions when he pleaded guilty to the resequencing of his son Julian's DNA. 

Captain Kathryn Janeway once joked with Ensign Harry Kim that he would make a good Starfleet JAG officer. 

Non-Starfleet persons, including non-Federation citizens, could lodge formal complaints with the JAG that arose from actions taken by Starfleet personnel. This included challenging the validity of a health and safety inspection. 

Background information
When asked why Richard Bashir, a civilian, was prosecuted by Starfleet, Ronald D. Moore commented, "The Trek universe has established certain things and one of them is a legal system that is clearly different from our own in many ways. Having Picard represent Data in a hearing where he has a personal history with the presiding judge and where his own first officer is the adversarial counsel doesn't make a lot of sense to me either, but 'Measure of a Man' is still one of the very best TNG episodes... In 'Dr. Bashir' I had the JAG deal with the plea bargain because we've never seen any form of judicial forum in the UFP that isn't Starfleet related... Trek has said time and again that Starfleet is more than just a military entity and seems to have police and/or judicial functions, so the involvement of the JAG seemed plausible."