But the scientist generally wins less than a programmer who has a moderately good year making a website to help people find other people who would like to walk their dog.
It's almost impossible to sell a public good. A career that focus on them will inevitably get less monetary rewards than one that focus on excludable ones.
A scientist gets less money than a programmer because despite creating much more total wealth on average[1], nobody needs to pay to get a share of the results, thus nobody has much of an incentive to pay.
As a related concept, life ain't fair.
[1] Does he really? I'm going with the common opinion, but I'd love to see data on that.