In a solid the electron energy levels spread into bands. What matters is the energy gap between the filled valence band and the conduction band. A conductor has no gap, an insulator has a wide gap and a semiconductor has a narrow one, so heat or light can lift electrons across it. A warmer semiconductor therefore conducts better.
Doping adds carriers: Group V atoms give free electrons (n-type) and Group III atoms give holes (p-type). The crystal stays electrically neutral in both cases.
A p–n junction conducts in one direction only, which makes the diode a rectifier. A transistor has two junctions; a small base current controls a much larger collector current, so it can amplify or switch. Switching circuits form logic gates.