Please search Google. We cannot possibly tell you "How to do" CGI in a thread on this forum! There are thousands of sites discussing CGI,
Perl, PHP et al on the internet. Also; if you need to run CGI scripts on your own server you will need to looking into Apache,
Perl, maybe PHP - again Google is your friend.
Free servers that allow CGI? You'll be hard pushed to find somebody who will offer such a service - many free hosting providers offer CGI as a purchasable add-on. Seeing as via CGI what you're doing is executing scripts on somebody's server, they need to be a bit careful about who they let (i.e. paying customers only) upload such scripts. Also; by executing scripts, you are consuming system resources on the server too.
If you need help with something more specific, please let us know in as much detail as possible!
Cheers
ZB