Spain advances with a win or draw. It could also advance with a loss if Germany wins or draws. Japan advances with a win. If it draws, it would advance if the other game also ends in a draw. It could also sneak through with a high-scoring draw if Germany wins without scoring too many goals.
Costa Rica advances with a win. If it draws, it would advance if Spain wins. Germany must win. Even then, it would only be sure to advance if Spain wins. If Spain draws, it will come down to goal difference with Japan, which is currently close.