I am not talking about the softwares like surf online, HTtrack, or any other 'save page' feature of browsers but I need to know how actually it happens in the background. I am interested in making my own program to do that.
Also is it possible to do in JavaScript. If yes what are the libraries I should look into or any other APIs that could be helpful. Please give me any kind of information about the topic I couldn't find any relevant thing to contribute my research.
Download entire HTML content of the web page 
For JS files, look for the <script> tags and download the js file using the src attribute or any inline scripts inside the tag
For CSS, use <link> tag to download the CSS file and also look for any <style> tags for inline CSS styling
For Images, scan for <img> tag and download the image using src attribute

Same approach can be used for audio/video etc
How to get all files like: js,css,images from a website to save it for offline use?