Sometimes things like that happen that are out of control. Perhaps it was some bacterial disease that spread from one to another; sometimes this can happen before you are even able to figure out what is going on.
But you did good for 5 months, so please don't let this discourage you from trying again. Just be sure to clean all your equipment very well before you use it with other fish.
There are other bettas just waiting for you to give them a good home.