(Note: the following is theory and should not be regarded as fact)
HIMYM has had to address the possibility of cancellation at least twice (and perhaps three times) so far in its run.
The first instance is something that's common to most scripted TV shows (a little TV industry primer follows):
When commissioning a TV series, broadcast networks usually put in an initial order to the production company for 13 episodes or so. If the series is launched in the fall and brings in satisfactory ratings, the network will order additional episodes for the season (aka "being picked up for the rest of the season," or "picking up the back nine" for a full season of 22 episodes). Otherwise, if the show isn't an instantly-yanked disaster, the network will allow the rest of the initial 13 episodes to be produced, and will typically burn them off (i.e. air the episodes even though it's a doomed, "lame duck" series), though in some cases some episodes are not aired on the original network and instead air on a cable partner, shown online, or put on the DVD release of the series.
So like any freshman series, HIMYM had an initial order of 13 episodes. And as with any series that have ongoing subplots, the writers of HIMYM were particularly motivated to provide a reasonably satisfying conclusion if those 13 episodes were all they were going to get.
And what was the 13th episode? "Drumroll, Please." And if you watch the episode, one can see where it would have provided a satisfying ending--Robin winds up unhappy, but one can see where she was instrumental in bringing Victoria and Ted together (a prerequisite of the whole story, at least if it were to end this early in the game), Lily and Marshall having a moment in the previous episode that they feel is a landmark in the journey to their wedding day, Barney being awesome as usual, and Ted and Victoria getting together in a very romantic fashion. All the episode was missing to become the finale of the show was a final voiceover from Future Ted saying "and that, kids, was how I met your mother"--something that could've been added had the situation required it.
The show was renewed for a second season before production on the first season was finished, so a "contingency" wasn't needed for the end of the first season--in fact, they made the season finale a cliffhanger.
The end of the second season wasn't as sunny, and the finale even aired before official word of HIMYM's renewal came down. So what we got in "Something Blue"--bittersweet but slightly reassuring and satisfying to some extent, with Ted's narration providing a little closure to the mother mystery without actually revealing the mother, and a memorable final punctuation mark (not a period, exclamation point or question mark, but a nice ellipsis) courtesy of Barney.
While HIMYM's renewal also came too late in the third season, it seems that the ratings boost from Britney Spears' appearance may have given the producers enough confidence in a renewal that they gave the show a little cliffhanger as well, though it's also conceivable that they would have been content with the "question mark" ending with the further assumption that Stella was the mother.