Numbers 20:1 reports the death of Miriam "in the first month." The year is generally assumed to be the fortieth and final year of the Exodus, because of a time marker given later in the chapter. Aaron's death is reported in verses 23-29, and we know from Num 33:38 that Aaron died on the first day of the fifth month of the fortieth year.
By this time the older generation of Israelites had died, as the subject matter of Numbers 19 reminds us. Verse 2 invites us to compare the next generation with the previous one. At a time when water was in short supply, Israelites were grumbling, as had happened back in the first year of the journey (Exod 17).
On that previous occasion, God had directed Moses to strike a particular rock, and water came out of the rock (Exod 17:6). This time God tells Aaron and Moses to speak to a rock, which again would be a source of water (Num 20:8).
In Numbers 20 God shows no sign of being angry with the new generation of Israel. They seem to be anxious to experience the blessings of the Promised Land rather than asking to return to Egypt.
Moses, on the other hand, loses control. Rather than speaking to the rock, he strikes the rock in anger. God still provides water, but he then lets Moses and Aaron know that they will not be entering the Promised Land. Instead they will die during that year.
Why did Moses lose control? Miriam's death could have been a factor. It also may be that Moses was emotionally exhausted after the forty years in the wilderness, and all the accumulated frustration of dealing with the Israelites for 40 years has taken its toll. He accuses the new generation of being rebels like their parents. By being angry with them he misrepresents God, who is not angry with them and wants Moses to take a patient approach.
Moses here might be compared to an old math professor who rants at a class of freshmen, "You freshmen are all alike! You come to university unprepared, you skip class, and you're trying to take calculus when you can't even do algebra." When a professor gets to that point, it is time to retire.
Here God does not punish Moses for a momentary slip-up. Instead, he recognizes that Moses is not the appropriate person to lead the nation into the Promised Land. He has served well and faithfully, and it is time to pass the baton to Joshua.