He probably learned to program at Starfleet Academy
Note, nowhere that I can find indicates that Kirk actually wrote code per-se, only that he reprogrammed the situation.
In the Alternate Reality (2009 reboot), and probably in the prime reality, Kirk was
a top-performing Academy student
(Source)
In fact:
According to his dossier at the official Star Trek movie website, as a
Starfleet Academy cadet, Kirk was top of his class in survival
strategies and tactical analysis and the assistant instructor in
advanced hand-to-hand combat.
(Source)
I pay close attention to the 'tactical analysis' part - now surely in an age when computers are so important such as the 23rd century, the need to understand how a computer works, or at least know some programming, would be pretty important in tactical situations.
Out of canon, TOS - Starfleet Academy novel: The Gemini Agent
During the Academy's "dead week" in May, Kirk experienced a mysterious
illness which left him with a six-hour gap in his memory. He was
charged with fifteen violations of the Cadet Honor Code and became a
suspect in a bizarre computerized attack on Cadet Pavel Chekov.
The excerpt I've emphasised indicates to me that, if Kirk was a suspect, it was probably well known that he knew how to program a computer.
Also, consider how computers are interacted with in TOS; it seems to be mainly based on voice-commands. We also see that programming doesn't really involve a 'language' per-se, at least that is what is indicated from The Menagerie when Spock 'reprograms' the computer:

So, Kirk probably needed some more skills than just talking to the computer to reprogram it in the Kobayashi Maru, but considering his education at Starfleet where learning how to survive is a fairly important skill considering the various situations you could find yourself in, there was probably a standard course in at least computer programming, although probably not 'programming' in the way we would understand it today!