This is usually caused by a bad thermocouple. The thermocouple looks like a thick, solid copper wire screwed into the gas valve on the water heater. The pilot flame heats the tip of the thermocouple, causing it to generate a minute electrical current at low voltage, which provides power to an electric valve that controls the flow of gas to the main burner. If you attempt to relight the pilot burner and it refuses to stay lit when you release the button on the gas valve a minute, or so, after the flame has been lit, the thermocouple is the most likely cause. Replacement thermocouples are readily available from most hardware stores and can be replaced by the homeowner, using simple tools, by following the instructions on the package.
Another cause of the problem could be a lack of air to the combustion chamber. Newer water heaters require more volume of air due to the addition of a heat sensitive diode to the thermocouple. Not enough air flowing through the combustion chamber to carry the heat up through the flue causes heat build up, which kills the thermocouple. On my water heater, I could light the pilot and the water heater would burn for 20 minutes or so and shut off. The fix included routing the flue straight through the roof and opening floor to allow more air into the water heater compartment.
Another possible culprit may be water-heater vent height above the roof. In some situations this may periodically result in a downdraft that could cause the pilot light to go out. If this is the case adding to the vent height may help. Note: building code states that the B vent required height is based on the roof pitch but it must be 2 feet higher than any portion of the building within 8 feet of the vent.
Make sure the vent cap isn't missing or rusted through on the roof, it will cause drafts and wind to blow out the pilot.