There are eight elemental gods representing aspects of nature, They are called Vasu. the Vasus are attendant deities of Indra, and later Vishnu.
These 8 Vasus were, one day, traveling on a holiday with their wives when they came across Sage Vasishta’s ashram. Outside the ashram, they saw “Nandini”, Vasishta’s divine cow. One of the wives was taken in so much by the beauty of the cow, that she requested her husband, Prabhasa, to bring the cow to her. Prabhasa replied “Dear, we are devas. What use do we have for cows or cow’s milk? Even though it is Nandini, whose milk gives everlasting life, we are already enjoying immortality on account of being Devas. Most importantly, Sage Vasishta is very fond of Nandini and it would be improper on our part to violate his integrity”. Despite many attempts by Prabhasa, his wife did not yield. She made imploring requests and melted Prabhasa ‘s heart. He agreed and thus, the 8 Vasus took Nandini and her calf by force and disappeared before Vasishta returned to the Ashram.
When Vasishta returned and found Nandini missing, he, through his divine vision, saw all that had happened and cursed the 8 Vasus to be born as Mortal Men in this world.
When the 8 Vasus came to know of this curse, they ran to Vasishta and fell at his feet asking for his forgiveness. Vasishta said that the curse cannot be lifted and has to follow its course. But the effect of the curse can be reduced. He said - Go request goddess Ganga to be your mother on earth and ask her to relieve you of your birth as soon as you are born so that you may return to the heavens without long years of suffering. This reduction in effect I grant to 7 of you who supported Prabhasa in his act of stealing. Since Prabhasa was the one who actually stole the cow, the curse will remain in full effect for him and he will have to live his full lifetime on earth like a man. But he will live a great life and be regarded as one of the best souls to have ever walked the earth. Saying this, Vasishta went back into meditation.
Relieved to hear this, the Vasus approached Ganga and requested her to be their mother on earth and throw them into the river as soon as they are born. Ganga agreed and came to earth and became Shantanu’s wife to carry out this task.
So, it all makes sense now why did she kill 7 sons and left the last one.
Reference: Mahabharata Chapter 03