I understand that there are many jobs in IT that seem more attractive than teaching, but surely that goes for maths, or chemistry, or whatever, too.
It doesn't, actually. It is much easier for an "OK" programmer, sysadmin, network admin, etc. working as a teacher in school to find a better paying job in the industry than it is for an "OK" mathematician, chemist, or physicist. Unless you work at the Max Plank Institute for Physics or the Fermilab, chances are most commercial organizations have more IT staff than they do scientists.
You also need to consider what education your teachers received themselves. Physics, math, chemistry have well-established curricula at most colleges. Comp Sci, on the other hand, is still very much work in progress. As every other problem with our education system, low quality of IT education in schools is caused by a combination of factors. I attended school in USSR. I had programming classes and the teacher sucked. In every other respect the school was top-notch. Entry-level teaching positions back in those days paid much better than similar engineering positions.
I think the most important factor is lack of established college programs that teach IT education, as opposed to just IT. Being a brilliant mathematician, for example, doesn't necessarily qualify one as a good math teacher. I consider myself a proficient programmer and sysadmin, but when someone asks me to explain something to them, after a couple of minutes I just want to punch him in the face. So, probably, a teaching career would not be the optimal choice for me.