As I have struggled with teaching my toddler to play by himself, I've discovered some valuable lessons.
1) Get everyone on board.
Your spouse, your child's grandparents, older siblings, etc...
2) Be patient, it will take time.
Think baby steps. Independent play does not happen overnight, expecially if you were like me and did not expect it from the start.
3) Reinforce behavior you want, ignore behavior you don't.
4) Find toys they like that keep them engaged.
Don't expect them to play by themselves very long with a toy that only requires them to press a button and listen to music. Toys that require manipulation and imagination seem to work best with my son. For example.... blocks, trains sets, play kitchens. (Read more here about what my son learns from his train set.)
5) Make sure your expectations meet where your child is developmentally.
For example, most twelve-month-olds are not going to be able to play well alone for thirty minutes. Do a little research and figure out what is reasonable to expect from your child.
6) Have a goal so you will know when you've reached it.
Do you want your child to play for ten minutes alone? Twenty? Figure out exactly what you expect so you will know when you and your child have achieved your goal.
Your child is not going to want or need you to sit beside her and text her boyfriend when she's fifteen, although you might want to! Eventually your child will learn to work and play alone.