Purpose of STP is to remove redundant paths in network. The redundant paths could be creating loops in network. Loop free path is created using root bridge. Loops in network are determined and links which are redundant are blocked. Algorithm used to execute this process is Spanning-Tree algorithm.
In this protocol STA chooses reference point called Root Bridge, all available paths are determined in reference to this root bridge. If there is redundant paths then it chooses best path and block other paths. Messages are exchanged between switches to gather information from all switches, messages are called Bridge Protocol Data Units(BPDU).
Spanning-Tree protocol is running by default on all ports of the switch. The spanning-tree protocol makes each port wait up to 50 seconds before data is allowed to be sent on the port. This delay can cause issues with some applications or protocols. To overcome this Portfast was implemented on Cisco devices. Portfast makes the port to enter the forwarding state by decreasing the time of listening and learning state.
Spanning tree protocol is always running even when the port is in forwarding state. It keeps on detecting loops in network. Portfast should be implemented when switch is directly connected to server or workstation and never to hub or switch.