The protection someone gains from having an infection (called natural immunity) varies depending on the disease, and it varies from person to person. Since this virus is new, we do not know how long natural immunity might last. Some early evidence seems to suggest that natural immunity may not last very long.
However, recent studies are saying immunity appears to occur in the majority of infected persons for up to 5 months.
Regarding vaccination, we will not know how long immunity lasts until we have time to observe more individuals who receive immunity from the vaccine.
Both natural immunity and vaccine-induced immunity are important aspects of COVID-19 that experts are trying to learn more about, and the CDC will keep the public informed as new evidence becomes available.