All perfect praise be to Allah, The Lord of the Worlds. I testify that there is none worthy of worship except Allah, and that Muhammad is His slave and Messenger.
The Hadeeth reads: “It is not lawful for a person to have sexual intercourse with a pregnant woman till she gives birth.” This is reported in the books of Sunan and Imam Al-Haakim graded it as sound.
There are other Ahadeeth with this meaning. For example, the Prophet said: “It is not permissible for one who believes in Allah and the Last Day to irrigate with his water the other's child.”; i.e., one should not have sex with a woman who is pregnant from another man.
A Hadeeth in Saheeh Muslim narrated by Abu Ad-Dardaa’ states that when the Prophet passed by a woman who was near the time to deliver a child, he asked about her and was told that she was so and so’s slave woman. He asked whether he was having sexual intercourse with her, and when he was told that he was, the Prophet said: “I am inclined to invoke a curse on him which will enter his grave with him. How can he take the child into his service when that is not lawful for him, or how can he make it an heir when that is not lawful for him?” [The reason for the curse is said to be because the man did not wait for the prescribed period after acquiring the woman]
Dear questioner, all the above Ahadeeth do not apply to having sex with one's wife; all these and similar Ahadeeth relate to a pregnant woman who is divorced or a captive. It is not permissible for anyone to have sex with such a woman with the exception of her husband.
In brief, a husband can have sex with his pregnant wife. But if any harm can be caused to her, especially in the last weeks of her pregnancy as advised by doctors, then one should avoid it. Know that the Sharee'ah does not approve of any kind of harm.
Allah knows best.