because as hand of the king he had learned of cersie's plot to kill Robert and all of his bastards, and he made the mistake of revealing his hand to her when he was not in a position of power to do anything about it. she pretended to agree to allow him to be exiled to the Wall, but had joffry kill ned.
And how did Ned learn of Robert's Bastard's?
This is Rhaegar, Viserys and Daenerys Targaryen
Notice anything?
Ned Stark was killed b/c he discovered Robert’s children weren’t his children and were products of incest between Cersei and Jaime Lannister. Instead of doing something about it right away by telling Robert, he tried to give Cersei a chance to get out of dodge since he knew Robert would have her, Jaime, and all three kids killed (he kept thinking about the murdered Targaryen children in the book). So he told Cersei what he knew, and she turned the tables on him quickly by having Robert killed and Ned thrown in prison.
He also told Joffrey to his face that he was a bastard. Ned was given a plea deal to go to The Wall, he took it, and Joffrey double-crossed him and had him beheaded.
He was killed because he was investigating why Robert Aryn had died suddenly after he and Catelyn had received a Raven from her nutty sister Lysa Aryn saying that Robert Aryn had been killed by the Lannisters. He found a book that had all the births of the nobles listed in it; and, he found the Baratheon section in which ALL of the children of Baratheons were born with black hair and dark eyes. Cercei's children had neither.
He had discovered that the "rumors" of Cercei and Jamie's love affair were true. His real mistake was showing mercy to Cercei and her children, which gave her time to take out Ned Stark and Robert Baratheon.