Khepri was a sun god, especially the god who "comes into being" (which is what his name means), so the morning sun.
His name was written with the sign of a beetle (kheper in Egyptian), member of the Scarabaeoidea in biology and therefore often referred to with the word scarab or scarab beetle.
The Egyptians observed that this beetle rolls dung into balls (which are used as a source for food or as brooding chambers). They saw a parallel with the sun disk, travelling across the sky and through the underworld, more specifically being carried through the underworld and being pushed above the horizon by Khepri. Scenes of Khepri in this context can be found in the last hours of the Amduat (a composition of texts and images illustrating the nocturnal journey of the sun), where he is ready to pass the border of the underworld at the end of the twelfth hour.
At the same time the Egyptians noticed that some scarab beetles live in dung; seeing the animal coming out of it again they were reminded of the sun coming out of the earth spontaneously. Khepri is usually depicted as a beetle or as a man with his head in the shape of a beetle.