It seems you may be unfamiliar with STEM Ph.D programs. The way it works is professors "sponsor" a graduate student to do reserach for them. The student will either work on existing projects started within that professor's lab or start their own related projects. The goal of this research is to contribute novel material into scientific literature.
It's also usually NOT a rich parent. For example a big funder is NSF or NIH, these institutions grant money to labs with the expectation that they will perform research.
Don't think of it as being paid to go to class, think of it as a job where you work 40 hours a week (usually way more) doing reserach, then go to class, then on top of that help teach those classes through being a TA.