Thionyl chloride (SOCl2) and phosphorus tribromide (PBr3) can be used for converting primary and secondary alcohols to alkyl chlorides and alkyl bromides respectively:

Both reactions have similar mechanisms with the idea of turning the OH into a good leaving group and then replacing it with the Cl– or Br– nucleophile via an SN2 reaction.
Let’s start with SOCl2:
Reaction of Alcohols with SOCl2
The alcohol reacts with SOCl2 to form an intermediate that is deprotonated by pyridine in the next step. These two reactions convert the OH group into a good leaving group and the nucleophile (Cl–), generated in the same step, then attacks the intermediate in an SN2 process:

Sulfur dioxide (SO2) and Cl– are the leaving groups of this substitution reaction.
There are studies suggesting that the role of pyridine is also to ensure the inversion of the stereogenic center via the SN2 mechanism. If pyridine is not present, the reaction tends to go via an SNi (nucleophilic substitution with internal return) mechanism. This is covered nicely in this article on Master Organic Chemistry.
Ask your instructor if it would be acceptable to show the reaction by an SN2 mechanism with the inversion of configuration.
Reaction of Alcohols with PBr3
In a similar reaction, phosphorus tribromide, PBr3, is used to convert 1° and 2° alcohols to their corresponding alkyl bromides with inverted chirality.
The initial reaction of the alcohol with PBr3 turns the OH into a good leaving group, which is then expelled by the Br- ion in an SN2 process:

SOCl2and PBr3 do not work for tertiary alcohols because of their steric hindrance. Remember, tertiary carbons cannot undergo an SN2 reaction, and they react by the SN1 mechanism.
The Advantage of SOCl2 and PBr3 Over HX Acids
At this point, you might have a fair question to ask, which is why not use an acid like HCl or HBr to achieve these transformations? Especially because they do work for tertiary alcohols.
And the answer is, it is certainly an option, and hydrogen halides are used to convert alcohols to alkyl halides:

There are, however, a few factors to be considered. First, some molecules are not stable under acidic conditions, and it is best to avoid them if possible.
The second factor is the stereochemistry. A lot of the time, chemists spend days or months synthesizing a molecule with a chirality center(s) and losing it in the process of preparing the final compound can be very painful.
Now, while primary and tertiary alcohols are not at risk of losing stereochemistry since the carbon is not chiral, secondary alcohols may go in the SN1 path, thus losing chirality:

Third, which is related to the second factor, is the possibility of carbocation rearrangements for the secondary alcohols when the reaction goes by an SN1 mechanism:

Once again, the use of SOCl₂ or PBr₃ allows to keep control over the stereochemistry and prevents rearrangements as the substitution of the OH group with the halogen occurs by an SN2 mechanism:

This is analogous to using mesylates and tosylates to avoid possible loss of chirality, and rearrangements when alcohols are reacted with HCl, HBr, and HI for converting them into alkyl halides:

In summary, we can say that SOCl2 and PBr3 are great candidates for converting primary and secondary alcohols to alkyl halides since they work in mild conditions and are suitable for chiral alcohols to prevent rearrangements and loss of stereochemistry.
Check out this 65-question, Multiple-Choice Quiz with a 3-hour Video Solution covering Nucleophilic Substitution and Elimination Reactions:
Nucleophilic Substitution and Elimination Practice Quiz

