Because, despite his illegitimacy, he was his father’s only son. Robert of Normandy had never married and had never had any children through legitimate means, so given the choice between a bastard boy or a bastard girl (there was a daughter by another mistress), or worse, no one at all...
William had been prominently associated with his father while Robert was alive. A fairly deliberate effort seems to have been made by Robert to make sure that everyone knew this IS my son, this is who I want you all to follow. For instance, William is clearly named on ducal charters that Robert signed, which would be somewhere you would normally expect the heir’s name to be. Shortly before his death, Robert gathered Norman lords specifically to get their fealty for William. Robert ALSO got the king’s support for William, as well as that of his uncle the Archbishop of Rouen...which means that when the time came, William had some very powerful friends willing to back him.
William DID have some trouble taking power. His illegitimacy created problems, and the fact that he was still very young (he was only seven or eight when his father died in 1035) didn’t help him, especially after the Archbishop died and William couldn’t count on that support any more. There were multiple uprisings and revolts. Several of the people who held his guardianship as a boy died violently, including the notable case of Osbern, who is supposed to have died in William’s bedroom. William actually had to leave Normandy for his own safety at at least one point - Guy of Burgundy masterminded a revolt in 1046, and William fled to King Henry for protection. The two of them returned with an army a year or so later to get the duchy back.
From 1035 to 1047, William was a minor, with guardians squabbling over the right to have him in hand. From 1047 to 1054, he was an adult who had to assert his right to control over Normandy through almost constant warfare. It was only in about 1060 that things finally settled for good.
His claim to England is a lot murkier, but basically derived from his father’s family anyway - he and Edward the Confessor (king of England before Harold Godwinson) were distant cousins, as a daughter of a PREVIOUS Duke (Emma of Normandy) had married into the English line. If he was recognised as his father’s son, then by default all of Robert’s kin were his kin, you see?