There are three distinct functions of layer 2 switching : address learning, forward/filter decisions, and loop avoidance.
Address learning :
Layer 2 switches and bridges remember the source hardware address of each frame received on an interface, and they enter this information into a MAC database called a forward/filter table.
Forward/filter decisions :
When a frame is received on an interface, the switch looks at the destination hardware address and finds the exit interface in the MAC database. The frame is only forwarded out the specified destination port.
Loop avoidance :
If multiple connections between switches are created for redundancy purposes, network loops can occur. Spanning Tree Protocol is used to stop network loops while still permitting redundancy.
The root bridge is the bridge with the best bridge id. With STP, the key is for all the switches in the network to elect a root bridge that becomes the focal point in the network. All other decisions in the network – such as which port is to be blocked and which port is to be put in forwarding mode – are made from the perspective of this root bridge.
BPDU : All the switches exchange information to use in the selection of the root switch as well as in subsequent configuration of the network. Each switch compares the parameters in the Bridge protocol Data Unit that it sends to one neighbor with the one that it receives from another neighbor.
The bridge ID is how STP keeps track of all the switches in the network. It is determined by a combination of the bridge priority (by default 32,768) and the base MAC address. The bridge with the lowest bridge ID becomes the root bridge in the network.