When light strikes Q1 (phototransistor), if the light is intense enough, Q1 is turned on, then current flows into the base of Q2 which also turns Q2 on, then what happens? Where is the current through the relay coil coming from
The current in the relay coil is coming from VCC, because Q2 does turn on, and conducts that current to ground. – TimWescott
how does the 2nd circuit work?
The transistors on the first circuit is acting like normally open switch. So when there's no light shining on Q1 there will be no current through the base of Q2 and prevent the relay current to flow.
Contrary to the first one, the transistors on the second circuit is acting like normally closed switch. When there is a light shining on Q1, its collector and emitter terminal is shorted to ground, thus there will be no base current flowing to Q2, preventing the current to flow. When there is no light, the base current can flow again.
In case you're wondering about the diode, the relay coil is an indcutor and the current from an Inductor can't instantaneously change, the diode purpose is to prevent inductive spiking and thus protect the Q2 transistor, read more here or watch Afrotechmod's video on it here
I've build a simulation for you using falstad, you can open it here.
I'm using PWM signal on npn bjt with led to simulate the light and phototransistor
As you can see the the second circuit working principle is the opposite as to the first one.