The job of a web server is to accept http requests and serve up http responses (see http request response basics). There are number of different web servers out in the world but the basic concepts are the same among them all.
When a request comes in to the web server, the Host header is examined and the request gets routed to the correct internal server. Host based routing is what allows a single web server to host multiple websites.
If no file exists, the web server can be configured to forward the request to another application for request processing. If you are building a web application then the request gets forwarded to your application. Your application can then examine the request and decide how to handle it. This could involve saving items to a database, communicating with 3rd party web services, or rendering dynamic html views.
Web application frameworks have been built to make the process of handling request easier and creating custom web applications easier.