The Spanning Tree Protocol (STP) is used to prevent loops and ensure a loop-free topology in Ethernet networks. It allows switches to create a logical tree-like structure by selecting a root bridge and determining the shortest path to reach each switch in the network.