Assuming you really know HTML (can hand-code it) and you know what CGI is (please read the
CGI specification otherwise although rather dated) as you previously mentioned, then you should learn Javascript and DOM, and probably Perl too if you are not too familiar with it.
It's difficult to give you any specific pointers as I do not know how much you know about all of these things. However, as you do not understand how the script works, then I really doubt whether you have production knowledge in HTML and CGI, as this is a really simple example.
You can do a directory or Google search on these keywords and you ought to find a lot of resources. You may probably wish to buy some books on these subjects but I have no recommendations at this point.
I would suggest you to ensure you have working knowledge of the client tier first, i.e. HTML and Javascript/DOM. Then you can proceed further to understand how to generate them on the server side, and investigate how CGI allows you to capture form parameters on the server-side, and send back the response.