Some of the data itself is from the public record, but compiling all of it yourself would be prohibitively expensive. It would take many trips to various libraries, churches, courthouses, etc. in lots of far-flung places in different cities, states, and countries. And that's all before you start indexing, searching, copying, or scanning any records.
Then consider they're hosting all those billions of records, have developed decent software for both building family trees and documenting them with their database of sources, and continually improve their software and increase their database holdings. The only reason they can charge so little is the economy of scale based on their huge userbase.
Look again. I said I thought it was reasonable for them to charge something for the service of having collected, scanned, indexed and hosted the data. What I did imply was that I thought the charge was excessive for what they're doing. What you may not realize is that much of the work done to transcribe and index the data is done by volunteer labor, people who do it for the love of genealogy, or to help make data available, or because they then get some level of free access to it in recompense. It's not like Ancestry is paying people salary to do most of that work.
They incur expense in collecting each piece of data once. There is some small ongoing expense for hosting it, making it available, but they collect revenue for this endlessly on the same data. They're making sufficient profit to be able to buy out other companies on the market (most recently, Footnote.com was picked up) so I can reasonably assume they're fairly flush with cash.
It would be much more expensive to me to travel or snail mail to all of the places that hold data I am looking for, I'll grant. However, Ancestry's fees are high enough that I still can't afford to maintain a subscription, so the data is lost to me anyway. Couple that with my local Library's subscription to HeritageQuest database, which contains some of the same data sources, and now Ancestry gets no money from me at all instead of what they might get if the fees were lower. Yes, I miss some of what they have available that other sources do not, but this is a hobby, after all.