First, be sure your butter is fully softened. Firm butter results in grainy frosting. Second, thoroughly whip your butter before adding the powdered sugar. Five minutes is a good rule of thumb. Finally, add the sugar in one cup at a time and whip for at least a minute after each addition.