It's known as a morality tale. These tales aim to impart wisdom and guide behavior through engaging narratives. They can be found in various forms of literature and folklore across the world.
A story used to teach a moral lesson is typically referred to as a parable. Parables present simple, relatable situations to illustrate a moral principle. They are common in many cultures and religions.