The same way the government funds any kind of project.
Contractor says: we can do this for $X.
Government says yes or no to the project.
You do the same for patent applications.
It's basically answering the question "how much is this technology worth to society?" If they don't pay for it, then the technology will end up as a trade secret.
This is actually already a part of the patent system, but not in the way you're thinking. If a contractor says publicly, "We can frotz quibbins with a turgut for $X," that counts as putting turgut-based quibbin-frotzing "on sale". This means that ⓐ nobody else can patent turgut-based quibbin-frotzing thereafter, and ⓑ the contractor themself only has a year to file a patent on turgut-based quibbin-frotzing from the date of that notice. This is called the "on-sale bar".
The idea is that patents are only supposed to be granted for novel, nonobvious things. If nobody had ever realized that turguts would be advantageous for frotzing quibbins, then you're an inventor if you figured out how to do it, and you can get a government-granted monopoly. But presumably if you're offering to sell someone a turgut-based quibbin-frotzing product, even if you haven't actually built it yet, you must be pretty sure it's workable and worthwhile. And now anybody else can go ahead and start frotzing the quibbins with a turgut now that you've pointed out how, and although the patent law gives you a bit of a grace period to patent it, it doesn't give you unlimited leeway.
If it turns out that the turgut has to be freebled first, then you (or anybody else) might be able to patent the freebling part at any later date, since that wasn't in the original offer to sell. But only if the need for freebling wouldn't be obvious to one skilled in the art.
So, basically, patents are intended for things that are not known to be possible until someone figures out how to do them. Lemelson got away with some pretty shameless exploitation of the loopholes in these criteria, a lot of which are now closed.
How do we measure the value of anything objectively.
But there is a world of difference between, 100's of people working for a decade on a problem kind of patents. And someone sitting on their couch for an afternoon patents. You seriously want to protect the former. And not really the latter.