Rough day in court because the judge threw the book at you? “The book” in this idiom refers to the full list of applicable laws, that is, the all the laws you broke and penalties you’re facing.
Have a great reason why you’re innocent, like being far from the crime scene at the time in question? That’s your alibi, which is Latin for “elsewhere.”
Aspiring attorneys take the bar exam, hope to join the local bar, then get disbarred if they’re too naughty. All this bar business goes back to Middle Ages England, where a physical barrier in the courtroom (often an actual bar) separated attorneys authorized to argue cases before a judge from spectators and others. Hence, the English “barrister” was one who had been “called to the bar” and had this courtroom role. To this day, many courts still keep some separation (think of that little gate) between spectators and the seating places for the judge, jurors, attorneys, witnesses, and courtroom personnel.