Even the greatest people, the most pious people, can make mistakes. Even the Prophets, peace be upon them all, who are protected by Allah from sinning, can sometimes make mistakes.
The great thing is that whenever a person realizes that he has made a mistake and sincerely apologizes (or repents) to Allah, Allah is willing to forgive his or her sins. This is because Allah is the most Merciful, and extremely forgiving.
One story that shows the benefits of repentance (that is, apologizing to Allah about one’s mistake or error) is the story of Prophet Yunus (alayh as-Salaam). He was a great Prophet sent to the people of Nineveh (which was a city where the modern-day Iraqi city of Mosul is).
Prophet Yunus preached the message of Allah to these people for a very long time, but they did not respond well to his call. When he felt that his preaching was not going to make them change their ways, he took off in anger. He boarded a ship filled with other passengers. After some time, the ship ran into a storm. The people on the ship cast lots, with the idea that the person whose name shows up should be thrown overboard. The name of Yunus (alayh as-Salaam) came up in the drawing three times, after which he threw himself into the sea.
Allah commanded a huge fish to swallow Yunus (alayh as-Salaam) without hurting him or breaking his bones. Yunus (alayh as-Salaam) realized that he had made a mistake by leaving his town and its people without the permission of Allah. This was why he was now imprisoned in this strange way in the belly of a fish in the depths of the ocean.
Upon realizing his error, he repented sincerely to Allah, and called out to his Lord in the following words: “Laa ilaha illa anta subhaanaka, inni kuntu min adh-dhalimeen” (There is no God but You, Glorified be You! Truly, I have been of the wrongdoers).
Allah was so pleased with his repentance that He brought Yunus (alayh as-Salaam) out from the darkness of the fish’s belly, and the fish threw Yunus (alayh as-Salaam) on a flat and plain shore. At that time he was weak and sick. Allah caused a plant of gourd to grow over him, and by His grace and mercy sent him to a hundred thousand people who all believed in him. The supplication that Prophet Yunus made was loved so much by Allah that it not only earned him his release, but is recorded in the Qur’an and was praised by the last of the Messengers, Muhammad (sall-Allahu alayhi wa sallam).