The idea is that in times of extreme stress or when facing death people are more likely to believe in, or hope for, divine help. But there are problems with the aphorism.
First, the aphorism isn’t true, for clearly many people die as atheists.
Second, even if the aphorism were true and foxholes were populated exclusively by theists, that says nothing whatsoever about whether theism is true.
Moreover, if true the aphorism really reveals that the source of religion is fear. And that doesn’t reflect well on religion, although it has made it a very profitable endeavour.
Source: reasonandmeaning.com