While the existing answers are great and cover a lot of the difficulties in vaccine development, I feel that they fail to address (or at least sufficiently emphasize) the fundamental misconception at the core of the question:
If that's the case, why does it take so much time and effort to develop a vaccine, for example, against covid-19?
The answer to that question is simple: developing a vaccine does not take much time. What takes a lot of time is testing the vaccine and making sure that it does not have any unforeseen side effects.
For example, let's take a look at the Pfizer/BioNTech COVID-19 vaccine, since it's been in the news lately. To save time and research effort, I googled for "pfizer covid vaccine timeline" and found this article from Financial Review, whose timeline I will further summarise below:
- March 17, 2020: Plans to develop the vaccine are announced.
- April 29, 2020 (six weeks later): First tests on human volunteers begin. At this point the vaccine development is essentially done, and the vaccine has presumably also already passed initial tests using in vitro cell cultures and animal test subjects to ensure that it at least seems to be doing something and doesn't have any obviously bad side effects. (The first human trials are done with four different variants of the vaccine, because the developers obviously want to minimize the risk of having to start over from scratch in case one specific variant turns out to be ineffective or dangerous.)
- July 1, 2020 (two months later): Preliminary results from the first human tests are announced. One particularly promising candidate vaccine out of the four initial variants is chosen for further testing.
- December 2, 2020 (five months later): The UK is the first country to approve the vaccine for use through a "rolling review" system, which allows a novel vaccine to be temporarily approved for emergency use in an epidemic even while it still undergoes further testing. Requests for similar emergency use authorizations are currently under review in other countries, including the US and Canada.
So, basically, even under this extremely accelerated emergency testing and approval regime (which squeezed into months what would normally take years), the actual development of the vaccine took less than 20% of the total time from initial planning to approval. All the rest is just testing, testing and more testing.
Now, the other answers have already covered pretty well the reasons why all this testing is needed, but let me anyway give a quick summary for completeness:
Vaccines intended for humans need to be tested in humans, because that's the only way to be sure that they work and are safe to use for humans. Cell cultures and animal models can never give a fully accurate picture of how the vaccine will interact with the complete immune system in a real human body.
Because all human bodies are different, any new vaccine (or other medical treatment) needs to be tested with as many different people as possible, with as many different ages, ethnic backgrounds, pre-existing conditions, etc. as possible, to make sure that there are no harmful side effects that only show up in a small subset of the population.
Also, when testing a vaccine, one of the most important things that need to be confirmed is that it actually prevents the disease. But since it would be grossly unethical to deliberately expose test subjects to a dangerous infection, the only way to test that is to vaccinate a lot of volunteer test subjects and wait for some of them to get naturally exposed to the disease (and then compare the vaccinated test subjects with an unvaccinated control group). This waiting takes time, and since (for most diseases) most of the test subjects will never get exposed and infected anyway, it means that a lot more subjects are needed.
Because human testing in inherently risky (but necessary), nobody wants to take the unnecessary risk of administering a completely untested vaccine to a large number of people. But a large number of people still need to be tested to confirm that the vaccine is safe for everyone. Because of this, testing is invariably carried out in multiple phases: first with just a small number of volunteers, then (if no severe issues show up in the initial tests) with a slightly larger group, and then with an even larger group, and so on. But since each new phase can only be started after sufficient data has been collected from the previous phase, this multiplies the already long duration of the testing by several times.
FWIW, most of the same issues apply to all new medical treatments, not just to vaccines. New drug development and testing is also notoriously slow and expensive for pretty much the same reasons — and even then there are plenty of cases where major side effects were only discovered when the drug was already on the market.
One issue that does specifically affect vaccines is that they're preventative treatments that must be administered to a large fraction of the healthy population, not just to already infected people. (Even only vaccinating specific groups at high risk isn't generally sufficient to provide herd immunity.) This means that their safety needs to be tested to a significantly higher standard (since many more people will be receiving the vaccine) and that, as noted above, determining their efficacy requires a must larger pool of test subjects (since not all subjects will be exposed to the disease).
Anyway, the upshot of all this is that if it weren't for all this testing and re-testing, we could've had a COVID-19 vaccine in April, or maybe earlier. (Moderna started their vaccine development already in January, and their first human trials in March.) But even in the face of a global pandemic, the testing is still necessary to make sure that the vaccines that eventually do get approved and widely used will in fact do what they're supposed to do, and that any side effects they may have don't turn out to be worse than the infections they prevent.
BTW, this need for extensive human testing to confirm the safety and effectiveness of a vaccine even after it has been developed is not just the reason why even one company will often develop multiple initial vaccine candidates in parallel, but also one of the main reasons (besides simply capitalism) why there are so many different competing vaccines being developed by different companies. Basically, even the companies that were late to start their vaccine development and testing could, for a long time, still hope to be the first ones to market if testing happened to turn up serious issues with their competitors' vaccines.
Of course, other reasons exist too: the second, third, fourth etc. companies to get their vaccine approved can also expect to win a decent share of the market, especially if their vaccine happens to be cheaper, more effective and/or easier to store and transport than the alternatives. Also, many countries may prefer to use a locally developed and produced vaccine if one is available in order to limit their dependence on foreign suppliers.