What Gandalf has to say probably has to be understood from Tolkien's religious point of view. The Lord of the Rings is subtly—but significantly—influenced by the author's devout Catholicism, especially on metaphysical issues like the nature and fate of the soul. Famously, the nature of the orcs (whether they could be born evil and yet possess all the mental capacities of humans and elves) was not something he ever could reach a satisfactory explanation of, which was one of the reasons that he never completed a definitive version of the Silmarillion.
When Galadriel feels that Boromir is "in peril," it is implicitly his soul that is peril. He is in danger of succumbing to the Ring's temptation. If he had taken the Ring from Frodo, he would have quickly fallen into the service of evil—either becoming completely Sauron's pawn and returning the Ring to its master's hand, or by trying (and failing) to make himself a new dark lord in Sauron's place. To have gone to the end of his life in such sin was, in the view of the devout Catholic Tolkien—and thus, in the view of the Wise within the narrative—a cause of grave sadness, and could lead to eternal damnation for Boromir's soul, had he died in such a state of sin.
Gandalf's other comments express his relief that Boromir was not able to take the Ring and then subsequently was able to redeem himself. Had Boromir seized the Ring by force, it would have corrupted him quickly, as it did Smeagol (who had murdered to get it). However, he failed to get the Ring from Frodo and so escaped that rapid descent into ignominy. Afterwards, he shows understanding that he had sinned, although he initially disguises this fact from the other members of the Company.
However, what most gratified Gandalf was that Boromir managed to recover and demonstrate the goodness of his soul, thus presumably dying in a state of grace. There is probably no act so worthy as willingly putting one's own life on the line to protect someone else. (Orthodox Catholic theology sees Jesus's crucifixion as an example of this, with him dying to save all other souls from Hell.) Boromir gives his life to defend two innocents (Merry and Pippin) from the orcs, and so, by virtue of this final act of selflessness, dies in a state of grace.
Throughout Book II of The Fellowship of the Ring, Merry and Pippin had seemed to be more hindrance than help to the Company. Gandalf's observation that their presence was fortunate for Boromir is for exactly the reason that you have inferred. Since they were there to be defended, there was an opportunity for him to redeem himself; had he instead died fighting only for his own survival, there would have been no equivalent redemption. Up to this point, it has not been clear what the two younger hobbits would contribute to the Company's mission. Although Merry and Pippin would subsequently prove their usefulness and personal valor, Gandalf does not know what is to come. He may be musing that their presence may have been one of those little-seen gifts of Iluvatar—that happenstance made them part of the Company just to give Boromir a last cause through which to win salvation.