Running back Nyheim Hines produced two stunning touchdowns off kick returns, the first 96 yards and the second 101 yards, to set up a play-off against the Miami Dolphins.
Hamlin has made encouraging progress since his heart stopped nine minutes into last Monday's game against the Cincinnati Bengals, but he remains in critical condition.
"All that was for him. I'm happy that the things happened to me, but his spirit was out there with us," Hines said.
"As soon as I crossed the goal-line I thought in my head 'wow, this is the first time since Cincinnati, I'm speechless'. I'm so thankful it's me to bring that juice but it's way bigger than me.
"As a community I feel like we needed that win. We needed some great energy and great vibes. We needed to win this."
Hamlin reacted to Hines' first touchdown, which was scored from the kick-off, by tweeting "OMFG!!!!!!!!!!!!!", before later adding about the win: "God Behind All This No Coincidence.."
The Bills' victory eliminated the Patriots from Super Bowl contention and the Green Bay Packers are another big name to miss out on the play-offs following their 20-16 defeat by the Detroit Lions.
The Philadelphia Eagles progress as the NFC's top seeds while the Kansas City Chiefs take the same position for the AFC.