This is not such an obvious matter, as in fact this consensus came about, to be specific that there will be global warming in the long run, before it was clearly happening, indeed, while the data from the previous three to four decades or so was that average global temperatures were falling. This was the situation in the early 1970s, when this all went down. And let me be clear that what most people hear is incorrect: it is not the case that there was always this consensus and that it was just a Newsweek cover and a few oddballs who were forecasting a possible ice age, or at least more modestly, that the future trend of average global temperature would be heading down as it had been for several decades rather than up. At a certain point in time it was a tossup in the scientific literature, but then the warming side won.
The year of the tossup was 1971. In that year,articles appearing in the top journals at that time publishing climatology (Science, Nature, PNAS, Journal of the Atmospheric Sciences, Geophysical Research Letters, Bulletin of the American Meteorological Society) were split evenly, with about a quarter predicting global warming, another quarter predicting global cooling, and the rest saying that it was unknown which way it would go. How could this have been and why were so many people pushing the cooling hypothesis (aside from the fact that average global temperatures had in fact been gradually declining for several decades)?
The issue was rising emissions of aerosols, aka "particulates" and their frequently related SO2, which tend to cool things off. As of 1971 most climatologists were highly aware that emissions of both the warming CO2 and the cooling aerosols/particulates/SO2 were going on. The major wave of clean air acts among high income nations that would limit emissions of aerosols and SO2 had not yet been passed, nor would it be known that while there would be limits on those, there would not be any limits any time soon on CO2 emissions. But that was not really the issue that decided the thing in the end.
As it was, views changed rapidly, and 1975 was the last year that there was an article in a top climatology journal still predicting global cooling (this was not too long after the much hyped Newsweek cover story), and 1978 was the last year that one saw an article in those journals that said it was up in the air. Since then, the scientific consensus at the top of the climatology profession has been that CO2 (and other GHGs) will dominate and we shall tend to have global warming (which allows for cooling in some specific locations, and of course there might be exogenous offsets such as volcanoes or changes in solar radiation). What happened during those years, with the turning around of the global temperature trend only starting around 1975 and still not clearly in place by 1978?
There are two things that were realized soon after 1971 (by 1972 the balance of views was already beginning to tilt). The most important is that aerosols/particulates/SO2 do not stay in the atmosphere all that long. They fall out in rain, often as acid rain, long a major pollution issue/concern, one of the reasons laws and regulations were passed to limit their emissions. This also meant that their effects were largely regional at most, not global. OTOH, CO2 takes a very long time to get removed from the atmosphere. So even if in 1971 increases in emissions of both aerosols and CO2 were roughly equal, this meant that the increase in ambient levels at the global level would be much more sustained for CO2 than for aerosols/particulates/SO2. The warming effect would win and dominate in the long run.
I would note the particular scientist who was probably most responsible for this change of opinion among scientists, the late Stephen H. Schneider, who died in 2010, and who would be one of the prime movers of the UN IPCC process. In 1971 he was probably the most prominent leader of the "it can go either way" camp, as highlighted by his very influential article with S.L. Rasool in Science, 133: 138-141, "Atmospheric carbon dioxide and aerosols: Effects of large increases on global climate." His change of view to the pro-warming side, which nailed in the broader shift and had been heralded in some earlier papers by him was his 1976 paper with R.M. Chervin in the Journal of Atmospheric Sciences, 33: 405-412, "On determining the statistical significance of climate experiments with general circulation models."
Now it is true that this consensus by the scientific community did not get much if any public attention for a long time, probably at least partly because of the overblown media coverage of the "threat of ice ages" that had been going on in some places in the early to mid-70s. Once burned (or frozen), twice shy. So it took James Hansen testifying before a Senate committee in the hot summer of 1988 to bring public attention to this, which with the hot weather gave credence to the already well established scientific literature, and he got the public credit rather than Schneider as the media piled on. Now to be fair, Hansen had been publishing in the ares in the 70s, but it was Schneider who was really the main figure who led the majority of climatologists to understand why global warming was probably going to dominate over global cooling, not James Hansen. I am not going to go on further about him here other than to remind people that his recent role related to the Paris accords has not been the most has nuseful.