Contrary to popular belief, some companions can, in fact, be killed.
From the wikia page regarding Fallout 4 companions:
Unlike Fallout 3, companions are marked as "essential" when acting as active companions, meaning they cannot be killed. If incapacitated, they will automatically heal after combat ends.
This leads to the following point:
Some companions lose their essential status when dismissed. They do, however, remain protected like other settlers and cannot be killed by NPC attacks (only by the player or their active companion or related action)
Some companions lose their essential status upon reaching the lowest level of affinity and leaving the player character.
It appears that the "killable" companions are the following:
Danse, X6 and Deacon are the only companions who can be killed by the Sole Survivor if they're not following the Survivor.