The same way I can take photos of my neighbourhood without leaving my house; light travels from the surrounding houses, through my windows, and I capture that light in a photograph. No need to leave my house.
The stars you can see at night would be like the houses in my street. The Milky Way Galaxy would be like the sky-glow I can see coming from the middle of the city I live in. I can see all of those things without leaving my house. On a dark night, I can even see sky-glow from the next town over, which is a bit like seeing a distant galaxy.
In the same way, we don't need to leave Earth to get photos of things in outer space; light travels from those objects, through the Earth's atmosphere (which is like the windows in my house), and we capture that light in photographs.
At the moment (and for the near future), we don't have any spaceships which can travel outside the Galaxy -- which would take millions of years at the speed of our fastest ships yet -- and our first interplanetary spacecraft (the Voyager probes, launched in the 1970's (before I was born), have only just left the Solar System. It would take them tens of thousands of years even to reach the nearest star (but they're not even aimed in that direction).