A virus can either participate in a lysogenic cycle or a lytic cycle. What decides that?
It depends on a few factors, such as how many phages infected the cell, whether or not the cell is in good growth conditions, and so on. If the cell is in stress or has low amounts of nutrients, the lysogenic pathway is typically activated.
The underlying mechanism has to do with a protein cascade involving either the cro or cI protein that is encoded by the virus. The cI protein is a repressor, and it will prevent the lytic genes from being transcribed. By default the virus will transcribe the lytic genes, so they must be repressed for lysogeny to occur. Similarly, cro is also a transcriptional repressor. The two proteins work in opposition to each other. cro binds to an operator, oR3, that is involved in repressing cI, which may prevent cI from being expressed and thus preventing it from repressing lytic genes (however the importance of this is debatable because if you replace oR3, the cell can apparently still lyse).
There are numerous other proteins, such as N and Q, that are involved. The N protein has to be transcribed by the polymerase ‘anti-terminating’, or reading through a termination signal. This will happen more frequently when the protein RNase III is present at high concentrations. The N protein is a lytic regulator. Thus, when there are high amounts of RNase III there will be more N expressed which leads to the lytic cycle. RNase III is not a viral protein. It is a host protein and the host expresses more of it when nutrients are abundant. This is how the virus is able to ‘sense’ if nutrients are high enough to enter the lytic cycle.
The whole system is way more complicated but this is it in a nutshell.