Typically, hamsters live 2-3 years. There is variation in the lifespan between the different breeds of hamsters. Roborovski hamsters, the smallest hamsters kept as pets, have the longest average lifespan and live to be 3 - 31/2 years old on average, and sometimes significantly longer. The dwarf hamsters, such as the Cambell's Russian Dwarf hamster and the Syrian hamster have the shortest lifespan and only live 1-2 years on average. The most common hamster sold in pet shops is the Golden Hamster which is also called the Syrian Hamster or the "Teddy Bear hamster" or "Black Bear hamster." Its lifespan falls in the middle and usually lives 2-3 years on average.
Generally speaking, hamsters live longer in captivity than in the wild. This, in fact, is true of many animals. You can increase the lifespan of your hamster by providing vitamins in their water and taking extra good care of them in general. Clean their cage frequently to prevent disease. Change the water frequently as well and don't allow it to get slimy as this could also give your hamster an infection. Supplementing their diet with fresh food such as kale, spinach, lettuce, carrots, mashed sweet potatoes, and eggs will also help your hamster live longer.
There is some controversy regarding the age of the longest living hamster. Partly this is because there is a difference between unofficial records and official records. There are plenty of anecdotal stories of pet hamsters living to be 4-7 years old and even a few rare stories of them living beyond this, although these are hard to believe and are certainly not common at all. The official Guinness World Book of Records reports a hamster that lived to be a little more than 7 years old.