Edward Jenner, the inventor of the smallpox vaccine, observed that milkmaids (domestic servants responsible for milking cows each day) would not contract smallpox. This was due to the fact that they often caught cow pox, a much milder form of the disease, from the cows. Jenner diagnosed a milkmaid called Sarah with cowpox and decided to test his theory that it provided protection against smallpox. He carried out an experiment on an 8 year old boy by taking the pus from a cow's pox pustule and rubbing in into a small cut on the boys arm. This was known as 'inoculation' and was a common method of preventative medicine, however it was usually from pus from a human already infected with smallpox. Many would not survive the inoculation, and those who did found their health permanently affected. Some time later, the boy was then infected with smallpox and found to be immune. He went on to carry out the experiment on a number of other children including his own infant son, in order to collect enough results to publish a report on the vaccine in 1798. The word 'vaccine' was based on the Latin word 'Vacca' for cow, from which the vaccine was taken from. The experiment was actually carried out in 1796, therefore pre-dating germ theory by around 50 years instead of 150 years as you stated. The idea was ridiculed at first (yes, anti-vaxxers have been around since the eighteenth century), and arguments against included it being repulsive to infect humans with animal diseases, and physicians encountering difficulties in acquiring cowpox matter as the disease was not widespread. However, by the early nineteenth century, Jenner's vaccine had become commonplace in Europe as its benefits were recognised. It is worth noting that what Jenner invented was not the same as the vaccines we have today, but rather another form of inoculation.
As for your question about how they thought it worked, Jenner had developed this theory himself from the common folklore that milkmaids could not contract smallpox, observing it himself, then carrying out his own experiment to prove his hypothesis that cowpox produced immunity against smallpox. The theory and subsequent invention of the smallpox vaccine was also a personal research mission for Jenner as he was inoculated with smallpox as a child and, as a result, his health suffered for the rest of his life. However, Jenner did not have an explanation for how exactly it worked. The smallpox virus could not be seen under microscopes. Only after Jenner had carried out further vaccinations on other children, including his own 11 month old son, was he allowed to publish a paper on the subject, and it was not immediately accepted or adopted. Furthermore, as a consequence of physicians not being aware that germs caused disease, vaccine patients would end up being infected with smallpox anyway from those who worked in close proximity to others with the disease, or would contract other infections from the method of administration (such as unclean needles).
In 1980 the World Health Assembly declared that smallpox had been eradicated. It was once one of the largest child killers in history, and now it has now been wiped out thanks to Jenner's vaccine. It also paved the way for more vaccines to be developed.