The logic behind this 'last day of the month' calculation is that you get the month part of a given date, add one to it to get to the next month. Then subtract 1 day from the first of that month.
This can be written in one line and here it is:
SELECT DAY(DATEADD(d, -DAY(DATEADD(m,1,GETDATE())),DATEADD(m,1,GETDATE())))