How can I keep my baby from pulling away while nursing?

Watch your letdown. Sometimes when your milk comes out very quickly a baby can get too much in their mouth, they aren't the most coordinated at sucking and swallowing. Sometimes pulling away is their signal that the letdown is too fast for them. If this describes you try positioning the baby so that they nurse against gravity. If it is still a problem you can pump a little first then nurse.
No distractions. Making sure your baby is hungry for the feeding and breastfeeding in an environment that is soothing with limited distractions may be helpful.