How is the DPS value calculated?

The DPS value as you say is influenced by many things which aren't possible to show as a number.

As you point out, if you don't land any shots your DPS is zero and if you headshot everything then your DPS will be higher than shown on your character sheet. DPS is not feasible to calculate for stats like stability/accuracy as their impact will be specific for every player.

Add to that things like skills (pulses) dmg bonuses for different enemy types and it becomes very tricky to give 'one number'.

This video gives a good breakdown of why the DPS number is 'wrong' and goes a long way to showing the types of numbers you could use to calculate your own DPS. There is link to their DPS calculator in the video.

You can find the spreadsheet I mention during the video at:

https://docs.google.com/spreadsheets/d/1lOh5fD3l1xhh1NE8HG0iQfY-d2Q_3-Yi9euvx_MH4Lg/

The link to the mentioned website www.divisiondps.com is currently dead (and redirects to an unrelated advert).