I can offer some general possibilities as to why someone might behave this way, though I can't be certain if these apply to your husband's situation:
Fear of Mortality: Facing one's mortality can be incredibly challenging, and some people may react in unexpected ways when dealing with the knowledge of their impending death. Engaging in reckless or impulsive behavior, such as cheating, might be a way for them to temporarily escape or cope with their own mortality.
Emotional Distancing: Some individuals might subconsciously distance themselves emotionally from their loved ones as a defense mechanism when they know their time is limited. This emotional distancing could lead to them seeking intimacy or connection elsewhere.
Seeking Validation or Comfort: The prospect of death may cause feelings of inadequacy or insecurity, leading individuals to seek validation or comfort in the arms of someone else, even if they deeply care about their long-term partner.
Feeling Unfulfilled: After decades of marriage, some people may experience a mid-life crisis, reflecting on unfulfilled desires or missed opportunities. This could lead to seeking validation or excitement outside the marriage.
Emotional Turmoil: Coming to terms with one's mortality can be an emotionally tumultuous experience. People might act out of character during such times, seeking relief from their inner turmoil.
Lack of Communication: If you and your husband did not openly discuss his feelings about his impending death, he might have felt unable to express his fears, leading him to seek solace in an extramarital affair.
Please keep in mind that these are general possibilities and may not directly apply to your husband's situation. If you want to understand his reasons better, the best course of action is to have an open and honest conversation with him. Professional counseling or therapy can also provide a safe space for both of you to explore your feelings and navigate this challenging time in your relationship.