In the Fellowship of the Ring movie, Gandalf has a brief conversation with Frodo where he sets up the Sméagol plot arc (emphasis mine):
Frodo (disbelieving) He escaped the dungeons of Barad-dur?
Gandalf Escaped...or was set loose. And now the Ring has drawn him here...he will never be rid of his need for it. He hates and loves the ring, as he hates and loves himself. Smeagol's life is a sad story.
Gandalf catches Frodo's look of surprise.
Gandalf (cont'd) Yes...Smeagol he was once called...Before the ring came to him, before it drove him mad.
And we, the audience, are shown his full backstory in the prologue to The Return of the King.
However, in the films it's never explained how Gandalf learns this information. In the book he gets it from Gollum himself:
What I have told you is what Gollum was willing to tell - though not, of course, in the way I have reported it.
Fellowship of the Ring Book I Chapter 2: "Shadows of the Past"
But this is plainly not true of the movies, where Gollum has forgotten the name "Sméagol" until Frodo reminds him of it:
Frodo You were not so very different from a hobbit once, were you...Smeagol?
Gollum [Looks up slowly.] What did you call me?
Frodo That was your name once, wasn't it? A long time ago.
Gollum My name... My name... S... S... Sméagol...
So, my question: in the movies, how did Gandalf discover Gollum's history?
Please keep answers grounded in some sort of canon; I welcome speculation, but speculation that's based in the script, interviews with the cast and crew, or other out-of-universe materials. "We just don't know" is also an acceptable, though disappointing, answer.