High in the Alps near the border between Italy and Switzerland is the Great St. Bernard Pass, used by humans to cross the mountain range since the Bronze Age. As they headed north to conquer somebody or other, the Romans erected a temple to Jupiter there. In 1049, Bernard of Menthon (canonized St. Bernard in 1681 and confirmed as patron saint of the Alps in 1923) built a hospice on top of the temple ruins as a shelter for travelers.
A group of monks maintained the hospice, took care of guests, acted as guides through the pass and acted as search and rescue teams for travelers that had gotten lost or injured. At some point, the monks began to train their dogs, who were brought from the villages in the valleys below to serve as watchdogs and companions, as rescue animals. (It's not clear when dogs were first brought to the hospice or when they were trained for rescue purposes, since the hospice was destroyed by a fire in the late 16th century and its archives were lost. Historians best guess from outside sources is that dogs first arrived at the monastery between the 1550s and 1660. The oldest surviving written reference to the dogs, the monastery prior's account of the cook harnessing a dog to an exercise wheel of his own invention to turn a cooking spit, is from 1707). The dogs, with their strength, weather-resistant coats and superior sense of smell, were well-equipped to guide and rescue travelers.
The Saint Bernard we know today is the result of centuries of breeding at the hospice and the surrounding areas. The family tree likely starts with the mastiff-type dogs brought to Switzerland by the Roman armies breeding with the native dogs which of the region. By 1800, the monks had their own kennel and breeding program, a melting pot that combined Great Pyrenees, Great Danes, bulldogs, Newfoundlands and others. The dogs of the hospice were well known in the region and were variously referred to as Barryhunds (in tribute to Barry, a dog that saved 40 lives), Sacred Dogs, Alpine Mastiffs, Alpendogs, and Hospice Dogs until 1880, when the name "St. Bernard" was officially designated.
What about the barrels?
The barrels we see around the dogs' necks in paintings and cartoons is the invention of a kid named Edwin Landseer. In 1820, Landseer, a 17-year-old painter from England, produced a work titled Alpine Mastiffs Reanimating a Distressed Traveler. The painting portrays two Saint Bernards standing over a fallen traveler, one dog barking in alarm, the other attempting to revive the traveler by licking his hand. The dog doing the licking has a barrel strapped around its neck, which Landseer claimed contains brandy.
Despite the fact that brandy wouldn't be something you'd want if you were trapped in a blizzard — alcohol causes blood vessels to dilate, resulting in blood rushing to your skin and your body temperature decreasing rapidly — and that the dogs never carried such barrels, the collar keg stuck in the public's imagination and the image has endured.