The 21-year old will leave Ibrox when his contract expires at the end of the season and link up with Die Rothosen on a four-year deal.
Bates, who has not played since injuring an ankle against Celtic last month, initially joined Rangers on a loan deal from Raith Rovers in 2016 before making the move permanent.
The centre-back became a regular under Graeme Murty and has made 17 appearances for Gers this season.
"HSV is continuing its personnel planning for the future and shaping the new team squad," Hamburg said in a statement on their website.
"For the upcoming season, the club can announce their first signing, David Bates, centre-back of Glasgow Rangers.
"Bates, who has been on duty for Rangers in the Europa League, is now transferring to the Elbe on a free transfer and wants to continue his positive development here."
Hamburg sit second from bottom in the Bundesliga table, five points behind Mainz in 16th place.