Your question suggests the answer. Jails are very often a revenue source. States and the Federal government often provide funds to build and operate the facilities. Courts impose fees on defendants to cover expenses. And of course there are all sorts monetization schemes that work when people have no options: