Spanning Tree Protocol (STP) is used at layer two to prevent bridging loops and broadcast storms. STP is needed when switches are configured with redundant links, in such a way that circular pathways through the network are possible. STP selects one switch as root bridge, and then picks the most direct pathway from the root bridge to each other switch. These selected paths form a spanning tree - a topology in which each switch has an unique, unambiguous pathway to each other switch. By definition, such a spanning tree has no loops, eliminating the possibility of bridging loops or broadcast storms.
References: