Could be one of several things. A thermocouple would be the most likely cause. It's used to determine if a pilot flame is present, and will shut off gas supply to the main burner and the pilot light. The pilot light going out could also be a safety thing. Most are designed that if the water exceeds 195 degrees, they go out. Could be a build up of dust on the base ring filter or flame arrester. Some are even designed to shut off if it senses flammable vapors, i.e., gasoline, which would be the worst case scenario, requiring replacement of the entire unit. May also be poor combustion caused by a blocked vent. Sounds like you are going to need a service call.
Thermocouple is the reason why it is not staying lit. How old is the heater? If its more then ten years, it is really time to update the heater. However this part can be fixed. It is a matter of simply removing that part and replacing it with one of same size. The thermocouple is the tiny rod like device that has a small copper tube, 1/16 inch or so that goes from the flame area and enters into the gas valve in most cased the top. When a flame hits it, it generates a very tiny voltage that is amplified inside the gas valve. When you push down the gas valve to lite the pilot this produced voltage creates a electromagnet that keeps the gas turned on so it will lite. If the pilot goes out, it allows the safety within the gas valve to turn off so you do not fill the hot water heater with unburned gas. If your handy, it is something you can fix on your own. Just be sure that is properly secured and installed according to the directions and ends up looking like the one your replacing. Part is about $20.