I don't think anyone has the answer for this yet. There was some evidence early on (I don't know what's happened with that since) of people testing negative...and then testing positive again weeks later. It's hard to know if that was an issue of them still being infected and actively having the virus (but the testing wasn't working right). So maybe they had always been positive, but the first test was a false negative. It's also possible that the second test could have been a false positive. It's also possible for contamination of samples to occur, especially in a community testing scenario that is fairly chaotic, lots of people working long hours and making mistakes, instruments not properly cleaned, etc. Labs produce errors in results all the time, so who knows?
But it's also possible that someone could have be re-infected. We know it's possible to become 're-infected' by multiple strains of all kinds of infectious diseases, so it's certainly possible it could happen with covid. Maybe you have immunity to strain A but then you got exposed to strain B, so maybe that wasn't enough to make you sick (you have partial immunity because of strain A), but you might still test positive. Maybe you could also get sick with multiple strains, one after the other.
In terms of vaccines, this is why vaccine development is so tricky. We have some viruses we just haven't been able to create a successful vaccine for (HIV is a really good example of this). Or the 'common cold' (though that isn't just one virus). Then there are other things were the vaccine has to change as the virus mutates. The influenza vaccine is an example of this: they have to make a new influenza vaccine pretty much every 6 months (for the southern hemisphere and then the northern hemisphere) because we don't develop immunity to influenza from becoming infected with it, and the vaccine changes so frequently that the vaccine from last year won't be effective still this year.
I don't yet know if anyone knows how it will work with covid. I imagine the scientists working on the early vaccine trials have some hunches. But the current trials are just safety ones, making sure there are no serious side effects. They won't really me measuring how efficacious the vaccine is just yet.