I want to understand these from the very beginning. Say I've written some library in some language, and uploaded them to github.com as public repository. Now anyone can see and use them. I cannot stop them from using it, while allowing them to see the code only, as anybody who can see, can also use it.
My question is, what is the purpose of a license for such code? Does it stop them from using it? Or if they can still use it, does it (the license) benefit me in any way? If yes, in what ways?
If I don't use any license for my source-code, is that a bad idea? To put it differently, would I regret in future if I dont use any license for my source-code which I release to the public?
What determines whether should I use any license or not? And on what basis should I choose a particular license for my code? How should I make any decision regarding licensing?
Please help me understand these things.
My question is, what is the purpose of a license for such code?
The purpose of a license is that you have a document where it is written down which rights you grant with your copyrightable work (software can fall under copyright).
This helps users of your software to understand which rights they have with the software.
A Free Software License allows users to use, study, share and improve your software. This is the same for those Approved Licenses listed on the website of the Open Source Initiative.
So without a license you do not give many rights. A license clarifies which rights are granted. This is the first benefit for you: Users of your software don't need to ask you first.
Does it stop them from using it?
No, a license is not stopping from using it. At least normally not. A license grants the rights needed to make use of the software therefore its actually the opposite.
Or if they can still use it, does it (the license) benefit me in any way?
What do you mean by still use it? They can use the software if you provide a (known) software license. The benefit for you is that you tell so it's clear what you give.
If yes, in what ways?
For example by clarifying that you provide the software AS-IS without warranties of some kind. If you look into the licenses you find a passage about exactly that.
If I don't use any license for my source-code, is that a bad idea? To put it differently, would I regret in future if I don't use any license for my source-code which I release to the public?
Yes, this is generally a bad idea because you more or less do not tell your users which rights they've got with the software. That can have negative influence on usage patterns and contribution patterns. That's something you might regret in the future.
See as well:
This is a good book available online which goes through a lot of stuff step-by-step, for example Choosing a License and Applying It - The book is called Producing Open Source Software How to Run a Successful Free Software Project by Karl Fogel.
And this is one of the best essays that basically tells nearly everything: Make Your Open Source Software GPL-Compatible. Or Else. by David A. Wheeler Released 2002-05-06, revised 2013-01-18 - it has a lot of very good links as well.