I'm not from the US so excuse my ignorance, but if law says it's legal, how is it possible that the judge doesn't grant it? Wouldn't that make it illegal for the judge to do so?

The law usually says it can be done but needs permission from a judge. This is like saying that an after-school activity can be done for children but needs permission from the parent. That doesn't mean the parent must give permission.

Judges are way too busy to officiate most marriages in the US.

Basically any adult can officiate a marriage, then its just a matter of filing the right paperwork with the county clerk - that is what constitutes the legal/civil marriage in the US.

There was the famous case of the clerk in a county in Kentucky refusing to certify same-sex marriages a few years back.

There is also something called "common law" marriages where the state considers you married even if you didn't file the proper paperwork, but were co-habitating and especially if you had children. But this is a dying practice and only recognized by a few states / territories (ironically Washington D.C. is one of them ...)

The law isn't "child marriage is always legal" but "child marriage is allowed in specific cases with a judges consent" basically. They usually need to be given a reason to make an exception