So I asked the same question to myself about 10 years ago and did a bunch of research.
Similar to shaving cream, or whipped cream from the pressurized can, the answer is related to Nitrogen. Nitro beer: It is a classification born from Guinness projects to simulate a freshly squeezed "cask conditioned" ale to all the people without access to cask ale.
The beers you describe are known as Nitro beers, and you see it more and more these days, like Samuel Adams Nitro White Ale, etc. Do not confuse Nitro beer with just plain old normal good head retention. Also don't get confused by the beers that are served "Nitro" AND also served normal (Smithwicks, for example). You can usually tell when its nitro or not, because of the tap. Nitro taps usually have long (often black) attachments on the tips.
Nitro tap tip:
![enter image description here](https://i.stack.imgur.com/gKYqR.png)
Non nitro:
![enter image description here](https://i.stack.imgur.com/UAiy1.jpg)
Nitro beer is pressurized with a special Nitrogen gas mix known as "G-Mix" or "Beer Gas" which is 25% CO2 and 75% Nitrogen.
Planet Earth's atmosphere composition dictates that CO2 will bubble-out of liquid easily, but Nitrogen does not. (already lots and lots of nitrogen in the atmosphere, who needs more?). Nitrogen creates stable bubbles, and CO2 does not (in comparison). Nitrogen has to be coaxed to come out of the liquid by 'bumping' some nitrogen molecules against each other.
So, to get that nitrogen "out of solution", Special taps with "sparklers" in them are used to help release the Nitrogen when serving, creating that creamy Nitrogen shaving-cream style head.
Or when its served from a can, a "widget" is used to create pressure difference when you crack the can, leading to the head being created. (Lasers are used to make tiny hole(s) the widget!). I used to think that these widgets had something special inside them, but they just cause a sudden pressure variance when opening the can.
Or you might use a Surger with canned nitro beer, which uses ultrasonic vibrations on the liquid, coaxing Nitrogen out, leading to that shaving cream head.
Once upon a time, the widgets were not plastic containers inside the can, they were syringes!
More notes: Some beer manufacturers that carbonate only using CO2, like Pilsner Urquell, also strongly suggest the use of Beer Gas when serving beer in cold places. They actually go so far as to change the gas mix recommendation depending on how cold the storage temperature is (50/50). The hotter the storage of the area, the more likely they will recommend pure CO2 for a tastier, colder beer.
When CO2 bubbles-out of a liquid it is Exothermic (it makes the liquid colder, and maybe so much that it feels like it burns your mouth and stomach).
So in colder places, Beer Gas is advised to be used to serve otherwise normal beer. Cheap bars are totally unaware of this advice while certain bars are likely required to adhere to this advice. (But I think there is a corporate witch hunt going on for violators). So you may see different style head in a "fancy place" in a cold city. Prague in the winter time?
![Text from Pilsner Urquell manuals](https://i.stack.imgur.com/gWCsW.png)
Finally, to get seriously off topic, there is a new Fad in Coffee Houses known as Nitro Coffee (and also Nitro Tea, for that matter). Cold-brewed coffee (or tea) that is charged with "Beer Gas" and served like a Nitro Beer. I won't even post a pic, because it looks exactly like guinness. The foamy head has a very nice taste, like crema on espresso (but more stable!). Try one sometime.