How does trigger file in recovery.conf work? Can someone explain how the trigger_file in recovery.conf works?

As far as I know the standby server will become the primary server when it detects the trigger_file. But how will the trigger_file be created when the primary server fails?
Any reliable failover scenario requires a third party, sometimes called a "witness", "arbitrator", "cluster monitor", "cluster manager" etc. to detect failure of one member and initiate a failover. This process ideally resides on a network separate from the one that connects cluster members to each other. It "pings" node(s) that are deemed to be masters or active nodes and triggers failover when they don't respond within the timeout threshold. In your case this monitor will create trigger_file to initiate the failover.