Different at different schools. And different countries too.
In the United States, the most common way is that applicants select potential advisors (but no commitment is really made) during the application process. After you accept the offer, some programs that start you with research right away will generally ask you to choose someone to start working with (i.e. RAship). Other schools will have you just work as a TA and take courses for awhile first. Most US schools will have you be an RA for awhile, maybe with the same professor, maybe with a different professor and you don't normally officially commit to and declare a thesis advisor until much later (although it's often pretty clear who it will be way before you declare it). For example, at my current program, we only officially declare our thesis advisor when we apply for candidacy, which is near the end of Year 3. But, during "rotations" back in Year 1, I only picked advisors that would be potential thesis advisors and after my qualifying exam in Year 2, my thesis advisor and I already informally agreed on it.
However, in many other countries, including Canada, it doesn't work this way. In these places, PhD positions are more like job positions and you are hired/accepted directly to a professor's group, instead generally to a department like in the United States. You will see a lot of European schools list PhD student positions on job databases the same way an American school might list a postdoc opening. In these cases, you know that you are applying to a specific advisor. For Canadian schools, you often do not see "job postings", however, most schools will require that you identify a thesis advisor during your application process and while a committee might check to make sure your application meets the department standards, it's ultimately up to the advisor to decide if they will accept you or not.