On top of my head you won't be able to call apex when the browser is closed in Native Salesforce. There is JS onunload method that you can try, but it might not always work according to W3Schools.
Note: Due to different browser settings, this onunload event may not
always work as expected.
In Native SF when Tab or Subtab is closed you will be able to do that by subscribing to lightning:tabClosed
lightning:tabClosed is an application event that indicates a tab has been closed successfully. This event is supported in Lightning console apps only.
<aura:component implements="flexipage:availableForAllPageTypes" access="global" >
<lightning:workspaceAPI aura:id="workspace" />
<aura:handler event="lightning:tabClosed" action="{! c.onTabClosed }"/>
</aura:component>