Terms of the deal were not disclosed.
Gaines, 25, was rated the NFL's 13th-best cornerback by Pro Football Focus last season.
Groin, hamstring and knee injuries caused Gaines to miss five games in 2017, but Buffalo Bills general manager Brandon Beane noted after the season that the team went 8-3 in games played by Gaines. He finished the season with 59 tackles, 3 forced fumbles, an interception and 9 passes defended.
The Rams selected Gaines in the sixth round of the 2014 draft. He has played in 37 career games (36 starts) and has 3 interceptions, 31 passes defended, 4 forced fumbles and 185 tackles. He missed the entire 2015 season because of a foot injury.
Gaines will be reunited in Cleveland with Browns defensive coordinator Gregg Williams, as the two previously worked together with the Rams.
ESPN's Mike Rodak and The Associated Press contributed to this report.