Spanning tree protocol (STP) is a standard designed to prevent network looping, network flooding, or what is sometimes called a "broadcast storm."
Let's imagine you have four switches connected to each other: A, B, C, and D. The switches are connected to each other in a loop: switch A is connected to B, B to C, C to D, and D back around to A.
If you wanted to send a data packet from switch A to C, you have two possible paths: A-B-C or A-D-C. Having two possible paths (called redundancy) is good because, if the A-B-C connection is broken for some reason, the data can still be sent through A-D-C.
However, having too much redundancy can create infinite loops when data is sent through all possible connections from the switch. As you can see, it isn't necessary to send the data packet through both A-B-C and A-D-C, but because the switches are connected in a loop, the data packet will be automatically sent through both avenues.
The best alternative is for the system to select the most efficient avenue and send it that way.
You can imagine with the addition of more switches, computers, and other network devices, the amount of data being sent through every possible connection can bog down a network exponentially.
To solve this problem, STP will block one of the connections, for example, C-D. Switch C can still communicate with switch D; it will simply need to send data the long way around, which would be C-B-A-D.
What happens if one of the remaining connections gets blocked? The blocked connection C-D can be programmed to "unblock" should that occur. The switches can still communicate with each other without flooding the network.