A
thermocouple is a metal rod with its tip sticking in a flame. If the flame goes out, the tip gets cold and turns off the gas.
If the tip is not in the flame, it gets cold and turns off the gas.
If the thermocouple goes bad (and they do) it turns off the gas.
If the tip is not in the pilot light's flame, move the tip of it directly into the flame. If it is already in the flame, it is probably bad. Follow the wire back to where it is attached to the valve or control board. (Take a picture so you can put it back like it is.) Take it off the board and take it with you to a big box hardware store, or a plumbing supply house and get a replacement. Put the new one back on like the old one was.
When a pilot light won't stay lit after releasing the button, the thermocouple is ALWAYS the first suspect.
They don't cost much, $10 should get you one. As cheap as they are, when I get that symptom, I just replace it.