"How was the first photo ever taken? What's the magic behind it?"
The first photograph that still survives until the present day is widely accepted to be Nicéphore Niépce's “View From The Window at Le Gras” (above), which was taken in either 1826 or 1827. Nicéphore's called this invention the “Heliographic process” presumably because the only light source with enough muscle to make a mark was the Sun.
It was a rather clumsy process by any standard, metal plates were coated with a chemical known as “Bitumen of Judea” and then placed in a light-tight box with a lens. As the light fell onto the plate the chemical responded to the light by hardening depending on how intense the arriving light was. After washing the plate with lavender oil the non-hardened material came away and only the hardened area remained. After this prints were made in the typical fashion for an engraving, application of ink to the plate and then pressure against paper.
Forget taking photographs of anyone using this method though, the bitumen was so insensitive that the estimates for the exposure length of View From The Window at Le Gras are at least eight hours, possibly several days, even the best models can only hold a pose for so long! Niépce collaborated with Louis Daguerre (who'll become very important) on a more sensitive process but while their efforts yielded some fruit, the new process still took several hours to make an exposure. Still no good for people.
After Niépce's death in 1833 Daguerre forged on alone, focussing on replacing the Bitumen process with a new process using silver halide emulsions. Daguerre was wildly successful and his new “Daguerreotype” process drastically cut exposure times from hours to mere minutes, allowing the first successful capture of a human in a photograph in 1838. This momentous occurrence happened while Daguerre was taking a photograph of a Paris street out of his window, the traffic on the street was moving much too quickly to have any effect on the photographic plates but one man having his shoes shined was stationary long enough to appear, and history was made (Click to enlarge the image on the left and see if you can spot him).
After further developments the required exposure time was reduced further and portrait photography was possible for the first time. Daguerreotypes were far from perfect (they showed a positive image on a piece of metal and copies could only be made by taking a photograph of the finished article) but they mark an important milestone in early photography.
Towards the end of the 19th century chemists and photographers developed methods to apply light sensitive emulsions to thin plastic films. These emulsions contained microscopic particles of silver halide suspended in a gelatin, allowing still greater sensitivity, the ease of use of film and the ability to print as many copies of a photograph as you like made photography considerably more accessible. The quest for increased sensitivity without loss of quality has been a driving force behind the development photographic technology since the beginning and continues now. Film photo prints work in a different way to both Niépce and Daguerre's processes.
When light touches hits the film surface it excites the silver halide which is subsequently turned into metallic silver during the chemical wizardry of the development process. This silver metal blocks light from passing through and appears black on the photo negative. To get a print a light is shone through the negative (and through an enlarger) onto a piece of paper treated with yet more of those ever-so-useful silver halides. The black portions of the negative prevent the light from passing through and therefore the black parts of the image are rendered on the print as the white of the paper. This process can be manipulated by blocking or applying more light to change contrast and tone allowing developers to produce amazing images like this one.