It depends on the OS, DNS server, and webserver that you are running. The principle is very simple. You need to make the required entries in the DNS and webserver config files.
The details of the required entries can either be found by reading the documentation for the servers that you are using, or by reading the config files and figuring it out yourself.
If you search there are some good articles on writing "control panel" type software which should give you an insight into how to do it.
Good luck