Varying WWW servers could implement the interface to external executables in different ways, however, the CGI interface has become the defacto standard way of doing it.
There are also infinite ways of determining whether a request should simply return a file or execute a gateway. The two most popular ways are to declare one (or more) directories on the server as containing only gateways (typically cgi-bin) or to use a file extension (such as .cgi) to indicate a gateway.
You may refer to the official description of the CGI or a local copy.
If it's not yet obvious, gateways run only on servers (so far). Since gateways run customized code on the server, there is also a security vulnerability. You wouldn't want to run unknown gateway code on your server unless your server has some method of protecting itself against programmer stupidity/maliciousness. This also means you would not want to support the ``.cgi'' convention in a filesystem that you don't control.
To be useful, submitting a form requires more than simply fetching a document. Therefore, a gateway is usually used to process the form data.
Most CGIs written for Unix are written in Perl -- a language well worth learning. Most of the CGIs in use here at IBM Austin are freely available: