The Morocco international, who can play at both right-back and on the right of midfield, has been in Real's youth system since 2006, and made 17 appearances in all competitions last season.
The 19-year-old will continue his development in the Bundesliga, however, as he seeks regular first-team football.
Hakimi was a member of Morocco's squad at the World Cup, and played all three games as his country were eliminated in the group stage, featuring for 90 minutes in the 2-2 draw with Spain.
"Achraf Hakimi is a young, very dynamic full-back, who has played for Real Madrid and the Moroccan national team at the highest level," said BVB sporting director Michael Zorc.
Hakimi will link up with Dortmund in the United States, with the German club set to get their pre-season preparations underway.
Dortmund finished fourth in 2017-18, 29 points behind eventual champions Bayern Munich.