c. 1300 (c. 1200 in surnames) "a jail, prison; a birdcage." The form in j- is from Middle English jaile, from Old French jaiole "a cage; a prison," from Medieval Latin gabiola "a cage," from Late Latin caveola, diminutive of Latin cavea "a cage, enclosure, stall, coop; a hollow place, a cavity" (see cave (n.)).
The form in g- was the more usual in Middle English manuscripts (gaile, also gaiole), from Old French gaiole "a cage; a prison," a variant spelling that seems to have been frequent in Old North French, which would have been the system familiar to Norman scribes. Now pronounced "jail" however it is spelled. Persistence of gaol (preferred in Britain) is "chiefly due to statutory and official tradition" [OED], and, probably, the fact that it is known the Americans spell it the other way.
In U.S. usually a place of confinement for petty offenders. The Medieval Latin word also is the source of Spanish gayola, Italian gabbiula.