Space is not flat. It is 3D, and we say that everything in it is held together on an imaginary surface we call spacetime. The idea of spacetime was put forward in Einstein's theory of relativity. When light travels through space, it moves along this surface.
Mass, such as stars and galaxies, sitting on this surface makes it dip. Imagine a piece of material being held stretched out. If we put objects, like plates or heavy balls, on top of it, then the fabric would dip lower in those places. The same is true in space. Where we have mass, we get a dip in spacetime. The more massive the objects, the bigger the drop.
Now think about what would happen if you tried to roll a ball along the material loaded with objects making dips. If the ball gets too close to one of the objects, it will get stuck in the gravity well surrounding it. Or the ball might make it past the object, but its trajectory will bend slightly. The same thing happens to light moving past objects in space.
The most obvious bending of light comes when objects are lined up directly behind each other. But the more massive the object, the bigger the curve in spacetime.

- How does Gravitational Lensing happen?
Lensing happens when light from directly behind a massive object, from our line of sight, can be bent around it. We call this gravitational lensing, and it is a way to see an example of the theory of relativity.
ImageCreditThis work by NASA/JPL-Caltech/T. Pyle (SSC/Caltech) is licensed under Creative Commons Zero v1.0 UniversalThe Herschel telescope and Earth are shown to the right. A foreground galaxy is shown in blue, located approximately three billion light-years away (its light took three billion years to reach us). A more distant galaxy, about 11 billion light-years away, is shown in red. The gravity of the foreground galaxy bends the light from the distant one, as shown with the red lines. The pink lines show what we actually see -- a distorted and magnified view of the distant galaxy. An example of a final image taken by ground-based telescopes is at the far left. Gravitational lensing bends the light around the foreground object, making background objects look distorted, often like small arcs of light. If these arcs form most of a ring they are known as Einstein–Chwolson rings.
ImageCreditThis work by NASA/CXO is licensed under Creative Commons Zero v1.0 UniversalA galaxy group nicknamed the Cheshire Cat, showing a large arc, or Einstein-Chwolson ring that appears to form a smiley face. - Uses of Gravitational Lensing
A cluster of galaxies makes a powerful lens. These galaxies don't just contain stars, gas, and dust; they are all held together in a giant halo of dark matter – adding to the mass. Images of lensed galaxies around these clusters have helped us find the dark matter and make a map of the Universe!
ImageCreditThis work by NASA is licensed under Creative Commons Zero v1.0 UniversalArcs of background galaxies being lensed by the cluster Abel 1689. Lensing also magnifies the light coming from the background object. We can use it to see things that are usually too far away and faint to observe without the lens. This has let us observe the most distant galaxies ever seen.
We can sometimes see mini versions of this effect, known as gravitational microlensing ,when a nearby object magnifies a star. Because these stars are closer, they also appear to move across our skies more quickly, meaning we might only see each one for a few days a year. But the magnifying properties of these lenses have been able to help find exoplanets in our galaxy.