In many cases they fight on knowing that death is preferable to capture, or for a greater cause, or because they are professionals. It's hard to pin it down to a single reason.
For example, some trapped Soviet forces in the Kiev pocket formed in Sept 1941 fought with unbelievable ferocity, fighting with their boots, their hands, their teeth in some cases. At that time, they probably did so because they were fighting for their motherland and perhaps their very existence as a nation. Later on, they probably fought to the death so not to end up in the hands of one of the Sicherungsdivisionen (Security Divisions) that followed the Wehrmacht into Russia or because they would be shot by their own officers if they retreated.