Over 2" diameter use 3" wood screws, anything less go with 2-2.5, measure first so you know you won't go through the pipe.
Best way to attach them is to go from the bottom of the wood into the pvc, that way you don't ride over screw or nail heads.
If it is a large diameter pipe (over 3") angle your screws and alternate the angle for each screw, that way you get a better hold, your screws won't bend (if possible) and it keeps the pipe stable with the wood.
For anything less just straight screw into it because there is less chance of it being unstable.