Rugby World Cup hosts Japan qualified for the quarter-finals for the first time in their history by beating Scotland 28-21 in a thrilling encounter in Yokohama.