Planets such as the Earth are located in a space environment that is governed by the activity of stars such as the Sun. The manifestation of stellar activity ranges from energetic magnetic storms, e.g., flares and coronal mass ejections, to slower, long-term variations in stellar radiation, magnetic field and plasma wind output. On the one hand, this dynamic activity of the Sun creates planetary space weather which impacts modern space-reliant technologies that humanity relies on. On the other hand, long-term solar-stellar variability has a profound influence on the evolution of planetary atmospheres and their ability to host life, i.e., habitability. In this talk, I shall describe our research to understand and predict the Sun's magnetic activity and its planetary impact. I shall also highlight how we are extending our understanding from the solar system to explore star-planet interactions and habitability in other exoplanetary systems.