When you pull the trigger on an AR15, and the bullet travels down the barrel, the rifle will use some of the hot gas to cycle the action.
This begins with those hot gasses entering the gas block from the gas port of the barrel, traveling down the gas tube, pushing the bolt carrier back, and extracting the empty casing.
As the bolt carrier group moves rearward, the casing is ejected, and the buffer and buffer spring compress, slowing the bolt to a stop and returning it to the forward position.
As the bolt returns forward, the magazine lines up a fresh round to be fed into the chamber, and the bolt catches it while returning to the forward position. Check out this article for a more detailed explanation of how the AR-15 sas system works.