Teams generally have the framework of trades planned out shortly in advance, and the trades are generally contingent on a few things happening. Like, "Hey, Washington. Seattle here. If a certain player is off the board at 5, would you be interested in moving up?" "Hey Seattle, we have a guy we're looking at. Here's what we are thinking compensation wise. We'll agree to move on up if he's still on the board at 5."
Seattle had a trade like this in place with Green Bay last year that fell through.
Many other trades are opportunistic and happen as the board begins to fall. The DK Metcalf one, as I've read, was a more opportunistic and spur-of-the-moment trade brought on by him falling unexpectedly far.