An analogy
Let's say you order a watch from Amazon. You place order, it gets shipped and after some days, it comes to your house. While you rest in your house during those days, it is not so much of rest for Amazon to get that watch delivered to you !
Let's understand how this system match a close pattern of what exactly happens in CDN (Content Delivery Network).
Amazon have warehouses from which it stores some of the products of different companies and categories and delivers them. You don't get a ship carrying the product from anywhere in the world to your house. Just like that, CDN have a whole network of servers, through which someone can get your website loaded on their device via any server within that network, geographically in the closest proximity.
Technical terms
CDN generally cache (stores) content in the servers and let the server deliver it to the closest user in that area that requested the HTML or JavaScript script, etc.
It reduces latency (time to deliver content) and host bandwidth.
Also, upto a very good extent it reduces DOS attacks.
More availability is the main feature focused in CDN.
Working of a CDN
There is a whole network of servers ready to serve data on demand.
There are points on internet (where every point represents an internet provider) that operates together to provide their server. These points are called IXP (Internet Exchange Points). These points are chosen strategically to reduce latency and increase access to users in any region of the world.
CDN also using file optimization which minimizes a file in order for it to transfer smoothly and in comparatively less stress.
Here are some points to understand how a CDN always keep your server up and running :
Load Balancing for dividing and distributing load efficiently across the network.
The failure of one server is handle by redistributing traffic to another server.
CDN have robust monitoring systems to analyze a traffic and act accordingly upon dealing that traffic with all servers.
Additionally, CDN ensures security with keeping up-to-date TLS/SSL certificates which gives an adequate amount of authentication and encryption.