CGI is typically used to front-end a database. If you wanted to display the results of a query or any other program, CGI is the way to do it. If a script, the script indicates the parser. The script gets input from environment variables. Script runs and sends legal HTML as output.
Server passes HTML output back to user's browser.
Since CGI basically allows the whole world to run a program on your system, security precautions must be taken. Many providers do not want to deal with this. Most that do charge extra for CGI accounts and often only allow CGI on business-class accounts. So, before you dive into this you might want to make sure you can use CGI at your site. You will probably need to work with a smaller, local ISP or with a university or other large company.