Part of the answer is that starships (even in the TNG era) are generally near a starbase so that if there were some need for a JAG you could go there and have your issue adjudicated. From The Measure of a Man
PHILLIPA: When I prosecuted you in the Stargazer court martial, I was doing my job.
PICARD: Oh, you did more than your job. You enjoyed it.
PHILLIPA: Not true! A court martial is standard procedure when a ship is lost. I was doing my duty as an officer of the Judge Advocate General.
And later (emphasis mine)
PICARD: Yes, I came to you. You're the JAG officer for this sector. I had no choice but to come to you.
Apparently all officers are briefed in some form with regards to legal matters. When Picard wants to have a trial about Data being a person, we have this exchange (emphasis mine)
PHILLIPA: Captain, that would be exceedingly difficult. This is a new base. I have no staff.
PICARD: But surely, Captain, you have regulations to take care of such an eventuality.
PHILLIPA: There are. I can use serving officers as legal counsel. You as the senior officer would defend.
PICARD: Very good.
PHILLIPA: And the unenviable task of prosecuting this case would fall on you, Commander, as the next most senior officer of the defendant's ship.
Another place we see the central JAG system would be in the TNG episode The Pegasus
PICARD: Yes, there is. (reading) Judge Advocate General's Report. Stardate 36764. Subject, inquiry into mutiny aboard USS Pegasus. Based on testimony from Captain Pressman and other surviving officers, the Judge Advocate believes there is sufficient evidence to conclude that certain members of the crew did mutiny against the captain just prior to the destruction of the Pegasus. Mutiny on a Federation starship? That's shocking. It's unthinkable. And yet you've never mentioned it.
Later (emphasis mine)
PICARD: I've spoken to Fleet Admiral Shanthi. There will be a full inquiry once we reach Starbase two four seven, and that will probably lead to a general court martial of Admiral Pressman and several others at Starfleet Intelligence. Your involvement in this affair is going to be thoroughly investigated, Will. There'll be some hard questions for you to answer.
This even extends back to the TOS era. Kirk was tried by fellow captains at Starbase 11 (although the Enterprise was already there). From Court Martial
KIRK: Captain's Log, Stardate 2948.9. The officers who will comprise my court-martial board are proceeding to Starbase Eleven.
And later
STONE: This court is now in session. I have appointed as members of this court Space Command Representative Lindstrom, Starship Captains Krasnovsky and Chandra. Captain Kirk, I direct your attention to the fact that you have a right to ask for substitute officers if you feel that any of these named harbour any prejudiced attitudes to your case.
KIRK: I have no objections, sir.
STONE: Do you consent to the service Lieutenant Shaw as prosecuting officer and to myself as president of the court?
KIRK: I do, sir.