Explanation
First, the LEN function counts total characters in the cell B5.
Next SUBSTITUTE removes all "line returns" from the text in B5 by looking for CHAR(10) which is the character code for the return character in Windows. LEN returns the result inside of a second LEN, which counts characters without carriage returns.
The second count is subtracted from the first, and 1 is added to the final result, since the number of lines is the number of returns + 1.
Dealing with empty cells
The formula in the example shown will return 1 even if a cell is empty. If you need to guard against this problem, you can wrap the formula in IF statement like so:
=IF(ISBLANK(B5),0,LEN(B5)-LEN(SUBSTITUTE(B5,CHAR(10),""))+1)
Mac version
On a Mac, the code for line break character is 13 instead of 10, so use this formula instead:
=LEN(B5)-LEN(SUBSTITUTE(B5,CHAR(13),""))+1