The temperature in space is indeed very cold, but it's important to understand that temperature is a measure of the average kinetic energy of particles. In space, the density of particles is extremely low, so even though the individual particles might have low temperatures, there are very few collisions between them.
In the case of the Sun, its high internal temperature and the process of nuclear fusion occurring at its core are responsible for generating an immense amount of energy. The core of the Sun is so hot and dense that it sustains a self-sustaining fusion reaction, where hydrogen atoms combine to form helium, releasing an enormous amount of energy in the process.
This energy is in the form of intense heat and light, which is radiated out into space as sunlight. While space itself is cold, the energy emitted by the Sun, including sunlight, can travel through the vacuum of space without being affected by the low temperatures.
On Earth, we receive a fraction of this energy as sunlight, which warms our planet and provides the conditions necessary for life. The Earth's atmosphere also plays a crucial role in trapping some of this energy and preventing it from escaping back into space, further helping to maintain a hospitable temperature range.
So, the Sun doesn't freeze over in space because it continually generates a tremendous amount of energy through nuclear fusion, which counteracts the cold temperatures of space and radiates heat and light into the surrounding environment.