Firstly, comic Grimlock was not as stupid as cartoon Grimlock by any means, but he had a speech impediment. He wasnt built on Earth either in the comics. The toy Grimlock has an intelligence tech spec of 7 too btw. It was the cartoon writers not Hasbro that made him dumb.
The first time: Optimus didn't name a successor, and the higher ranking Autobots chose Grimlock based on the traits he displayed fighting Trypticon.
The second time: Grimlock was the highest ranking Autobot on Cybertron and had proven he would take extreme measures for those he commanded, and that he wasn't going to be one who'd trust the surviving Decepticons and their talk of peaceful cooperation. (And he was right after all)
Also: Rodimus Prime didn't exist. There was a kid named Hot Rod who Prime was grooming to be a leader (based in part on meeting the time travelling Rodimus Prime if you include UK continuity) but wasn't ready yet.