The simplest rule I know is:
Use 两(liǎng) when the number is followed by a noun. Otherwise, use 二 (èr).
Examples:
- 二)二加二得四。
- 两)地铁晚两分钟。
Disclaimer:
I am not qualified Mandarin Chinese Teacher or native Chinese speaker. It means my notes about Mandarin Chinese may contain mistakes. If you spot any error, let me know in comments below.