From the Super Mario Wiki
The ghosts are species from Luigi's Mansion and Luigi's Mansion: Dark Moon.
The ghosts first appear in Luigi's Mansion. They were captured by Professor E. Gadd several years ago, but are released by King Boo, who also releases the Portrait Ghosts and Boolossus. Luigi chases after the ghosts and catches them all, but the specific Gold Ghost species appear several times in later games.
Different types of ghosts appear in Luigi's Mansion: Dark Moon, the sequel to Luigi's Mansion, however, these ghosts are friendly and help Professor E. Gadd. However, when the Dark Moon is destroyed by King Boo, the ghosts turn against E. Gadd and go on a rampage, commanded by King Boo. Luigi once again comes up behind them and not only fixes what they break, but also catches them all, and repairs the Dark Moon, making them friendly yet again.
List of ghosts
Luigi's Mansion: Dark Moon