Yes, you can buy your ticket ahead of time if you want - as long as the ticket hasn't been passed through the turnstile, it'll remain valid. But as others have mentioned, at least one ticket counter will be open at each station. You can also use the ticket vending machines (if you have correct change).