Is it used to reset the program which we flash and allow us to program
It seems you are confused about what reset actually resets. It does not "reset the program", it resets the MCU. The content of flash is not changed, so previously written program is still available.
Specifically, while NRST pin is connected to the ground the MCU stays in "reset state", i.e. it does not do anything, the clocks are stopped and the program that is already flashed is not running.
When you disconnect NRST pin from the ground (release the button) the internal pull-up resistor (marked RPU on the schematics) applies VDD voltage to it, which eventually flips Schmitt trigger and brings MCU out of reset state, at which point it starts executing the program at reset vector.
why is a capacitor connected on the outer side of the pin and to ground?
The capacitor is there to provide some EMS protection and avoid parasitic resets.
What is the use of it in microcontroller and Stlink? How does it work?
Usually the program at reset vector is a bootloader, which will either detect an attempt to upload new program (via USB, CAN, Serial etc. channels) or simply pass execution to the old program already in flash. It is the second case that can be interpreted as "program reset", since the program indeed begins execution from start. However the important difference is that NRST does much more than simple program restart, for example it resets all the MCU registers to their default states.
Note that in your particular case (using SWD interface for programming) the NRST pin is not actually useful, since SWD completely bypasses bootloader and can reset and flash MCU directly. It is only if your program reconfigured SWD pins as regular GPIO then you need hardware reset to flash a new one (remember that NRST resets all registers to default states, including pin configuration). ST-LINK usually uses software reset, however if it does not work you can go into setup interface and change this to hardware.
In short, NRST pin is used to activate bootloader when you need to flash new program using one of the communication channels and to reset MCU to default state when you want to flash new program using SWD interface and SWD pins are not available.