From the Super Mario Wiki, the Mario encyclopedia
After King Boo and Hellen Gravely spring their trap on Luigi and his friends, all of the buttons are taken out of the elevator's control panel apart from the button to B1, with the rest being distributed to powerful ghosts residing in The Last Resort. As Luigi progresses through the hotel, he must defeat the various boss ghosts to retrieve the elevator buttons and return them to the control panel, enabling him to reach the other floors and rescue his friends. In most cases, he will receive the button immediately after defeating the bosses, but some will require him to solve a puzzle or catch another ghost that steals the button before he can collect it (most notably Polterkitty).
When Luigi returns a button to the elevator, it vibrates out of his extended hand and screws itself into the control panel.
List of elevator buttons