Day and night occur because of the rotation of a planet around its axis. The hemisphere of the planet that faces the sun at a particular moment experiences daylight, while the opposite side experiences darkness. The Earth rotates counterclockwise from west to east.
Earth takes 23 hours and 56 minutes on average to make a complete rotation with respect to the sun, which adds up to a single day every four years. This is why leap years exist, so the lost day can be made up. Day and night have different lengths throughout the year, depending on the latitude of a particular location and what season it is. For example, summer in the Northern Hemisphere has longer days than nights. At the poles, the days and nights last for six months because they are always pointed either toward or away from the sun.
When a hemisphere of the planet is facing away from the sun, it does not receive sunlight, and night occurs because residual sunlight and heat from the previous period of daylight is being used up. When Earth rotates to bring that hemisphere back into the path of the sun's rays, sunlight once again heats and lights that hemisphere. Rotation around an axis is a feature common to all planets, all of which also experience days and nights of varying lengths. For example, a day on Mercury takes 58 Earth days and 15 hours, while a day on Uranus takes only 17 hours and 14 minutes.