A web server takes the client request and give response back to the client. It knows how to communicate with the client. The webserver gets the request finds the requird resource and returns something to the client. Something that resource in an HTML. Something can be a picture, video, soundfile, a pdf etc.
It avoids the overhead of socket programming to connect clint to the server. It contains the web container which is responsible for running your program in server side