I would guess that jails are to keep us safe from people who are violent or whom we don't like. We've always had these. Prisons, on the other hand, are probably a product of Enlightenment thinking that a rational reform system, through application of methodical discipline in mathematical proportion to the crime, can improve anyone and help make them part of society. It's a necessary component of the clockwork view of the better society, with nobody left behind. Combine this with Victorian ideas of public morality and the work ethic, and you have today's penal system: oriented toward reform, but permeated still by the wish to see misdoers suffer.
It depends on the country, but the modern focus is definitely on reform. When Karla Homolka can get a B.Sc. and M.Sc. for free in prison, it's fairly clear that punishment is not the government's priority. (I'm not saying prisons are too liberal, it's just an extreme example that people are guaranteed to find hateful.) However, the fact that jails remain seedy, unclean, violent places, as well as the continued use of capital punishment in certain backward countries, show that Thanatos is clearly alive and well.
I recommend Michel Foucault's Discipline and Punish. It's an account of the origins of the modern answer to this very question, and begins by examining both the daily schedule of a Victorian boys' reform institution and a graphic account of the prolonged and extremely horrible public torture-execution of an attempted regicide in absolutist France. To give you the basic idea, it turns out that the English were relatively quite kind and gentle with Braveheart. Tough reading, to be sure, but very interesting.