There are a number of reasons for this. Let us look at a few of them.
We felt that going to the moon was really important. We were in the Cold War with the Soviet Union and we thought it would make us look bad if they got to the moon first. We felt going to the moon would help stop communism from spreading and help democracy spread because it would make our system look good.
Americans do not really like antipoverty programs very much. Americans tend to believe quite firmly in the idea that people should not take handouts from the government. Therefore, antipoverty programs are never very popular.
It is not clear to everyone that antipoverty programs really work. Many people think that antipoverty programs like welfare actually hurt the poor. They think that the programs make the poor depend on the government instead of allowing them to go out and support themselves. It would not make sense to take a lot of money from the moon program and put it into programs that might not even help the poor.
So, we can say that it was a combination of our desire to defeat the Russians and our distrust of welfare programs that made us spend money on the moon landing rather than on helping poor people.