For all of these things, when managed properly (i.e., rotate programmers enough, take care of documentation, have strict coding standards etc), you can get a new programmer who has had a 2 year degree in programming and 6 months of training on the specific technology up to speed in such a project in a week. That's not quite as interchangeable as a guy turning screws in a car factory, but it's not that far off, either.
I realize that everybody likes to thing that they are so special and that without them the world (and certainly their company) would grind to a halt), but for the vast majority of programmers, it's simply not true. And for those where it is true, there is a large part where it's only true because management allowed one person to become so entrenched in one place that they've made themselves indispensable. And not because of the nature of the work.
And this is how crap code comes to be.
And when you need changes in 3 years time - hire a contractor on fixed fee, who cares that he will age 10 years in 10 days time; he'll move on to the next pile of crap soon anyway.
Which is to say: organizations derive such fantastic value from software, that even slow, buggy, late and over-budget projects, nor a parade of such projects, is enough to cause them to reconsider their approach.
Which is the answer to the original question.
Q: If software delivers so much value, why are programmers typically paid so little and treated so poorly?
A: Because even bad software delivers value far faster than most organizations can incorporate it and there's no shortage of bad programmers.
And of course there are exceptions, but from what I've seen it seems like there are more opportunities for programmers to bring personal creativity to their work than doctors and lawyers, just because the field is less established and there are more open questions.
Of course, there's the union/doctor's association thing that keeps the supply short whereas programmers and "programmers" turn up almost everywhere.
Similarly programming skills vary a lot. Doctors and lawyers execute more and innovate less and they all have a basic level of knowledge that people are willing to pay for. Hiring a programmer is like hiring an artist: a good one will create lots of wealth while many can just fill in the blanks with something.
There's also the question that many doctors and lawyers are running their own clinic or law office, or they're shareholders of their "employer". There are a lot of programmers merely on the payroll. And being on the payroll only doesn't make you rich unless you can negotiate your salary or compensation to match your perceived personal capability. This hints that doctors and lawyers could be more fairly compared to entrepreneurs.
One thing that may disadvantage us is that programmers are still one big blob: we don't define our specialities strongly enough to the outside world (or to ourselves). For example, teachers have their subjects: one might consider two maths teachers interchangeable but not a maths teacher and a French teacher, and that is obvious to a non-teacher. You and I might see the absurdity with swapping a web programmer for an embedded safety-critical systems programmer, but I don't think it is at all obvious to the outside world.
If management can't see the difference in value-add between a seasoned hacker and a new CS graduate, they won't pay for it.