CGI, the Common Gateway Interface, is a standardised way of writing scripts that the server will run when a request for the relevant URL is received. A gateway is typically a program that transforms information from one form to another, and one use for CGI scripts is to implement gateways. For example, you may have all your data in a relational database, and want to make this information available to the Web. To do this you would write a gateway script to transform HTTP requests into accesses to your database, and translate the replies into HTML.
Before CGI, each server passed the query information into a script in its own way. Unfortunately this made it difficult to write gateways that would work on more than one type of server, so a few of the server developers got together and CGI was the result. Some servers don't yet support CGI, but most of the popular ones now do.