In the book of Exodus God has Moses lead His chosen people out of Egypt and towards the Promised Land. Along the way the people are frustrated by the lack of water and grumble at Moses to do something. God tells Moses to strike a rock with the staff and God will provide water (v17:6). Moses obeys the command and it works. Later on, the people are grumbling to Moses about water again, but this time God tells Moses to merely speak to the rock in front of the people. God is still the provider but He wants it to be clear that now Moses can ask for it (Numbers 20:8–11). However Moses is frustrated by the peoples’ complaining (rebelling against God’s care and leadership) and strikes the rock again in anger. God provides the water but decides to punish Moses for his disobedience.
Some descriptions of this say God was setting up a prophecy of sorts about getting living water from Christ, the Rock. The first time Moses has to strike the rock, symbolic of the way the Messiah was abused and sacrificed (which is why we are able to get living Spiritual water). The second time Moses is to merely speak to the rock (in prayer?) to refresh the supply. Striking it again would violate the truth about how Christ only needed to be sacrificed once for all people and all time. So God punished Moses for disobeying the commands and corrupting the imagery.
This is a good foreshadowing of Christ for us to read now (except that Christians can ask God directly instead of through a human leader), but at the time God’s people couldn’t get that lesson because God did not provide an explanation of Moses’ error. That means the real lesson is for people reading the account later, who should not let a single person be their only source of truth about God, because even God’s chosen messenger (or other leaders) can be fallible.
There is another lesson that people then and now can gain from this. The second water event was near the end of the forty years of wandering in the desert, so Moses had already received the history of Genesis. Moses was at least partly punished because he ignored or forgot the lesson of God warning Cain about the danger of anger (Genesis 4:3-8). Cain became angry when God refused his sacrifice and then tried to teach him what a good sacrifice would be. God warned Cain that his anger made him vulnerable to sin, and sin wanted to possess him (which would block Cain from hearing God trying to guide him further). This led Cain into killing Abel. At the second water event Moses was angry with the stubborn people and let sin possess him. He voiced his frustration at needing to be responsible for getting water for people that he said were rebelling against God (Numbers 20:10) then struck the rock in disobedience (It is probably not an offense that Moses claimed he was bringing the water instead of God because in 20:8 God says that Moses will bring the water). So after all his experiences walking with God he ultimately failed because he was still vulnerable to the same weakness as Cain.
God was upset and told Moses he failed by not representing God properly to His people when he let his anger control him (Numbers 20:12). God blamed HIM alone for his failure (even though the people were rebellious God still wanted to provide for them) and punished him by denying his entrance to the Promised Land. Moses gave a farewell speech to the people and while warning them about their rebelliousness he showed his lack of repentance for his failure when he told the people it was THEIR fault that God was punishing him (Deuteronomy 4:21). This was Moses failing again by behaving like Adam and Eve just after the Fall (which he also knew the lesson of) when they pointed fingers at others for their mistake instead of facing the truth and owning up to it.