Ramanujan's reasoning is analysed in detail by Berndt, in his discussion of chapterschapter 6,7,8 of Ramanujan's notebooks. Ramanujan starts from the Euler-MacLaurin summation formula, $$\sum_{k=1}^x f(k)=C +\int_1^x f(t)\,dt+\tfrac{1}{2}f(x)+\sum_{k\geq 1}\frac{B_{2k}}{(2k)!}\frac{d^{2k-1}}{dx^{2k-1}}f(x),$$ and focuses on the constant $C$, which he uses to find the asymptotic expansion of $\sum_{k=1}^x f(k)$ as $x\rightarrow\infty$. This approach was made somewhat more rigorous by Hardy.
In this way Ramanujan obtained the notorious identities $\sum 1 =-\tfrac{1}{2}$ and $\sum k = -\tfrac{1}{12}$. For a $p$-adic interpretation of the latter sum, see this MO post.