How does a transistor work as a switch; internally what actually happens?
If you want the basic gist of how it works, then imagine a transfer of electrons from one material to another. I won't talk about any equations since that would take all day. 

Remember valence electrons in your chemistry class? When you apply voltage to this semiconductive material, holes will be created and whatever material around it will donate their valence electrons to those holes created.

User @jonk mentioned the "Microelectronics" book by Sedra and Smith. Well, here are some diagrams from that book.



This is known as an "n-type" where it "donates" an electron to surrounding silicon atoms.





This is known as a "p-type" where it "accepts" an electron from surround silicon atoms.





Placing these +5 or +3 atoms are known as "doping". The +4 atoms shown are silicon, which can either accept or donate their valence electrons because they have four electrons on their outer shell. There are other "+4" atoms like Germanium that is also used in transistors. The "+5" atom is shown to be on the right neighboring column of the Periodic Table of silicon and germanium such as arsenic or antimony. The "+3" atom is on the left neighboring column of the Periodic Table of silicon such as boron or gallium. The elements that I have told you about are typically used for solid state devices.

Moving on...



So when you apply voltage to this cluster of atoms, you create holes for electrons to move through.



Creating these holes creates conductive activity where there electrons or holes migrate from a higher concentration to a lower concentrations... THIS IS KNOWN AS DIFFUSION.





I know what you're asking: "But what is this threshold between conductive activity and insulate... (insulative?) activity? AKA ON and OFF respectively?"

I'm getting there. Now I have to talk about the depletion region. It's a little tricky to explain what this is but think of it as an area where holes and electrons come together like bread and butter. When you combine both n-type and p-types into a single "brick", it's known as a p-n junction.

This is what it looks like before you apply voltage (known as "equilibrium mode"):



and this is what happens when you apply either a positive or negative voltage (known as biasing):





Okay so we're finally finished talking about all of that so I won't explain exactly how this donation and acceptance of electrons will work in a transistor but just know that this is what a npn and pnp transistor look like:



and for an example, this is what happens when you apply some voltage to an npn:


NPN and PNP transistors are known as bipolar junction transistors, or BJT for short. There is a different type of classification of transistors known as FETs (field effect transistor, which is unipolar).





But I hope that explains everything you need to know about how transistors work on a very basic level. From here you should see how a transistor is "activated".