The pyramids were built primarily as monumental tombs for the pharaohs of ancient Egypt to ensure their survival in the afterlife. The ancient Egyptians believed that when a pharaoh died, he became the god Osiris, and for him to survive in the afterlife, his soul (or "ka") needed a physical place to rest along with his mummified body. The pyramids served as that resting place, housing the pharaoh's body and treasures to sustain him in the next world. They were constructed as part of a religious and cultural belief in eternal life and the importance of preserving the king's legacy and spirit.
Building pyramids was a massive, state-organized project involving thousands of skilled workers and peasants, supported by the wealth and resources of the kingdom, including the fertile land nourished by the Nile River. The largest and most famous pyramids were built at Giza for pharaohs Khufu, Khafre, and Menkaure, and they were designed with impressive architectural sophistication to symbolize the pharaoh's divine status and power.
In summary, the pyramids were built as enduring monuments for the pharaohs to facilitate their journey to the afterlife, preserve their bodies and souls, and immortalize their status as gods in Egyptian culture.