If you push on the sides of a square or rectangle it collapses. If you do the same to a triangle, it doesn’t.
So shape is very important for imparting strength. In fact a triangle is one of the strongest shapes and that's why you see them in roof trusses and beams.
You can strengthen a square by putting a diagonal strut across it which in fact makes it into two triangles!
A beam can be used to span a gap and support a load. To support a load the structure needs to transfer that load, through it's parts, to the ground, so that the ground eventually carries the load. If parts of the structure aren't strong enough and can't carry the load, the structure collapses.
A horizontal beam carrying a load is squeezed at the top (compression) but stretched at the bottom (tension).
Between the top and the bottom, the forces are much lower. So the beam can be lighter if unnecessary material is removed, as the highest loads are at the top and bottom. That's how the I beam (or rolled steel joist, RSJ) came about.
Concrete is strong in compression but weak in tension so a loaded concrete beam would split at the bottom. You can stop this by reinforcing the bottom of a concrete beam with steel reinforcement.
Providing that the beam can carry the load, a trussed beam is lighter than a solid beam too.
There are other ways of spanning a gap and supporting a load - the arch.
A load on the top of the arch is transmitted to the ground by compressing the arch, which is strong vertically but the base wants to spread sideways. So arches need to have a way of resisting those sideways forces eg. by supporting the sides of the base in some way, by building buttresses or even by joining arches together.
A dome is a succession of arches rotated through a whole circle.