Starfleets Legal and Justice system is not as complicated as many think.
I will try to keep this very simple to explain. But, essentially everything works around rehabilitation. Starfleet does not simply lock some one in a cell for years on end. Prisoners and Prisons are more relatable to Drug Rehab centers of the past.. The idea being to find what caused the individual to do what they did, and correct any problems. Allowing them to return to normal life after sentence. Prison Labor could be more related to " Landscaping" or "Maintainance" work.
There are, of course, those who need more help, or maybe cannot be rehabilitated at all. And while they are still held in high security centers, its still more of a hospital to help the individual as opposed to simple Jail Time. These individuals are often transferred of a more appropriate center on Earth.
If you are comparing Starfleets Legal System to that of today, the basis is the same. Just, its not about putting someone away in a cell for xx amount of years, but rather, lets see what we need to do to help this individual to get back to regular life. Where 2 people convicted of the same crime and given the same sentence, 1 may serve longer then the other depending on how the rehab goes.
However, that being said. It does take a lot for formal charges to get put forward where a court in necessary to pass judgement. Most cases are able to be dealt with from the Commanding Officer.
I am not a Legal Officer, so I'm not the best at explaining it. I apologize.
To put it personally, Starfleet doesnt want to put people away, its to much of a drain on resources to just lock someone away for however long. Starfleet wants to help rehabilitate people, so they can return to their lives. Sentences are not finite. Someone sentenced to 5 years of hard labour could find themselves out in 2 years if they rehabilitate properly.
The main idea of imprisonment being to keep the offender safe where they can receive help so that one day they can overcome their issues and return to a normal life.
Other things, such as returning to starfleet duty, is decided by a counsel of Doctors, Councillors, and senior administrative officers afterwards.
Judgement is still up to the Judge or Senior Officer in Charge, and can range from topic. However, as I said before. Sentences are not finite. Its all about what you do to change while you are in there. Prove it, and you can be pardoned. (Being pardoned from a prison term does not mean the initial incident is removed from your starfleet record, but officers will be understanding of the circumstances).
If you have any future questions about this, I can try to contact a Legal Officer for the official terminology on it all.