Creates threat of nxe5 and castles at the same time after d4 exd4 he gets rid of the pawn that could've been threatened. If exd4 white should play qxd5 because if nd5 then c5 and qxd1 will come . After queens are exchanged it favors black because open position for his bishops . This is at least what I think the reason is
Why is 5.O-O Better than 5.d4 in the Ruy Lopez Exchange?
Lasker used to play 5. d4 exd4 6. Qxd4 Qxd4 7. Nxd4 in some famous games against Tarrasch and Capablanca, but then it was discovered that after the agressive 7... Bd7 8. Be3 0-0-0 followed by Ne7 and Ng6 Black is more than fine. Since 5. d4 looks premature 5. 0-0 therefore is best : for instance 5. 0-0 f6 6. d4 exd4 7. Nxd4 c5 8. Nb3 Qxd1 9. Rxd1 Bg4 10. f3 Be6 11. Be3 b6 12. a4 is a much better endgame than what White gets after 5. d4 (where 6. Nxd4 is dubious because after 6... c5 White can't take back on d1 with a rook).
Sometimes I'll read articles about games in this opening but the annotators tend to just pass over it, only saying that Black is fine. But I really don't know the difference between playing d4 now and later.