In-Universe, there may have been any number of reasons why Gandalf took Pippin along with him to help the Gondorians.
Pippin had scarcely left Gandalf's side since they had arrived at Minas Tirith. Pippin had never been to a city of such size, the largest place he had been to was Edoras, which dwarfed in comparison. Maybe he didn't want to be left alone in such a big confusing place that he had only arrived at days earlier.
Likewise, perhaps Gandalf didn't want to leave him in such a place. Pippin was notoriously inquisitive and constantly getting himself into trouble (such as in the Mines of Moria, causing the Fellowship to get attacked when he knocked the skeleton into the well, or taking the Palantir and accidentally speaking to Sauron). It's possible that Gandalf didn't trust him to keep himself out of trouble whilst he was gone, thus thought keeping him close by would have been safer for him.
It's possible that Gandalf and Pippin were already on the way somewhere on the back of Shadowfax when they became aware of the soldiers fleeing Osgiliath, and he didn't want to waste anymore time getting Pippin off of the horse before going to the aid of Faramir & co.
Out of universe, it was so that Faramir could catch sight of Pippin for the first time in the presence of Gandalf, causing him to see the recognition in his eyes and coming to the conclusion that Faramir and Frodo had crossed paths at some point.
I can't remember how this interaction is approached in the books, but with the time constraints of the movies, I'm assuming that Gandalf learning the fate of Frodo needed to happen rather quickly, as it is in the following scene where Denethor is chastising Faramir for letting Frodo and the Ring go whilst Gandalf is present.
This scene would have made less sense if the audience had not seen Gandalf become aware of Frodo's fate beforehand, and was important to establish the relationship between Faramir and Denethor, who would realistically meet at the earliest opportunity, meaning that there would not have been the chance for another scene where Gandalf learns that Faramir has met Frodo before.