Spanning Tree Protocol (STP) is a network protocol designed to prevent layer 2 loops. It is standardized as IEEE 802.D protocol. STP blocks some ports on switches with redundant links to prevent broadcast storms and ensure loop-free topology. With STP in place, you can have redundant links between switches to provide redundancy.
To better understand the importance of STP and how to use STP to prevent broadcast storms on a network with redundant links, consider the following example.
SW2 sends a broadcast frame to SW1 and SW3. Both switches receive the frame and forward the frame out every port, except the port the frame was received on. SW1 sends the frame to SW3. SW3 receives the frame, and sends the frame back to SW2. SW2 then again forwards the frame to SW1! The same thing also happens in the opposite direction. Without STP in place, these frames would loop forever. STP prevents loops by placing one of the switch ports into blocking state.
So, our toplogy above could look like this:
In the topology above, STP has placed one port on SW3 into the blocking state. That way, if SW3 receives a broadcast frame from SW1, it will not forward it out the port connected to SW2.