"Immediately Jesus made His disciples get into the boat and go before Him to the other side, while He sent the multitude away. And when He had sent the multitudes away, He went up on the mountain by Himself to pray. Now when evening came, He was alone there. But the boat was now in the middle of the sea, tossed by the waves, for the wind was contrary. Now in the fourth watch of the night Jesus went to them, walking on the sea. And when the disciples saw Him walking on the sea, they were troubled, saying, 'It is a ghost!' And they cried out for fear. But immediately Jesus spoke to them, saying, 'Be of good cheer! It is I; do not be afraid.'"
According to John 6:15 - "Therefore when Jesus perceived that they were about to come and take Him by force to make Him king, He departed to the mountain again by Himself alone." Jesus had descended to the plain to feed the multitude, but after perceiving this mistaken desire of the people, He avoids the problem by dismissing everyone and going to the mountain alone.
Jesus made His disciples get into the boat and start heading to the other side of the Sea of Galilee while He sent the multitude away. The obedience of His disciples in leaving would help Him persuade the crowd to do the same. However, it should be noted that the Greek wording here is strong and seems to indicate that the apostles were reluctant to leave Him.
Jesus had wanted to rest and spend some time in prayer earlier that day, but thus far the multitude had made it impossible. However, after sending His apostles and the multitude away, He was then able to be alone with His Father in prayer for several hours (cf. Matt. 14:25). It is likely that His prayer focused on the following: (1) John the baptizer's death and His own approaching crucifixion, (2) the faith of His apostles and the multitude, and (3) the comprehension of all of His followers regarding the spiritual kingdom He would establish.
John 6:17 states - "It was already dark, and Jesus had not come to them." The apostles evidently expected that Jesus would come to them shortly after dismissing the multitudes. At first they probably stayed within visual sight of the shore, hoping that Jesus would hail them and come on board. However, as the hours passed and it became dark, they likely simply decided to cross the sea as Jesus had instructed them.
"But the boat was now in the middle of the sea, tossed by the waves, for the wind was contrary" (Matt. 14:24). The portion of the Sea of Galilee in which they crossed was approximately 6 miles wide. They were heading west and the wind was powerfully working against them. The apostles had traveled about halfway to their destination (i.e., 3 to 4 miles, cf. John 6:19). It is possible that they have some doubts regarding Jesus' wisdom at this time: Why had He sent them out into this predicament? Why had He walked away from the perfect opportunity to establish an earthly kingdom (cf. John 6:15)? The following miracles likely took place to reassure them that they needed to trust Him even if they didn't understand His actions.
"Then He saw them straining at rowing" (Mark 6:48). Here is miracle #1, which occurred around 3 A.M. (the beginning of the fourth watch). Whether He was still on the mountain or standing on the shore, given the distance and darkness, Jesus could only have seen this through miraculous means. He then started walking on the sea (miracle #2) but intends to pass them by (i.e., He doesn't head straight for the boat). Friends, it should be comforting for us today to know that we are never out of His sight nor beyond the scope of His care, even though it may seem otherwise (cf. Matt. 28:20; Heb. 13:5,6).
The disciples see a humanoid shape walking on the sea and become terrified thinking it is a ghost. No doubt their fears would have been even greater had Jesus been approaching the boat instead of just passing it by. "But immediately Jesus spoke to them, saying, 'Be of good cheer! It is I; do not be afraid'" (Matt. 14:27). Surely the apostles recognized the voice of Jesus. It would have brought them great assurance.