In 1666 (and perhaps earlier), when Newton was 23 - he had begun work on what he called “the method of fluxions and fluents,” effectively what we know as calculus. Newton’s discovery of calculus was mainly a result of practical use - he needed a method to solve problems in physics and geometry, and calculus was what resulted. On the other hand, Leibniz had become fascinated by the tangent line problem and began to study calculus around 1675.
The ideas of the two men were similar, although it is unlikely that either of them knew the specifics of the other’s work. The two men spoke in letters often, and discussed mathematics - and although the Royal Society found Leibniz effectively guilty of plagiarism later, this was not likely the case. Both men came to similar discoveries in different ways - Leibniz came to integration first, while Newton began his work with derivatives.
Although Newton discovered the principles of calculus first - he did not publish them until many years after Leibniz did. Leibniz published his first paper employing calculus in 1684, but Newton did not publish his fluxion notation form of calculus until 1693, and a complete version was not available until 1704! Nonetheless, Newton still came to the discovery first - and although both men are officially credited, Newton is the one that most people remember.
However,