You estimate it by using past experience. For example, If I'm to tell you that I need a login process implemented, how would you estimate it? Because a login can be a lot of things, not just two fields that you need to validate against a database.
First of all, you need to learn to ask questions. All kinds of questions in order to know what you're supposed to do. In this example above, you should ask me more about the login process itself - how the user is supposed to log in, what parameters is he going to use, what should happen if his account is disabled, should we allow log in using other accounts (like Google or Facebook). So - learn to ask as many question as you can.
Now when you know everything (or think you know everything), what you need to always have in mind is that 99% of the people do not know what they want and that is important because that is also a part of equation. With that in mind, I always tell you the worst case scenario in days (where 1 day = 8h) accompanied with a sentence "20% more or less" which should give you enough room to really finish it even if you screwed up with your estimate.
The third thing you need to know is that estimates change so it is not a big deal to tell a client that you are going to spend more time than you initially thought. But do not do it 24h before deadline, do it before you've spent 50% of the estimated time. If you communicate with your client, even if you miss a deadline and your original estimate, people won't mind. If you lie to them and then try to find some stupid reason why you're late (like - you had to fetch a medicine for your sick aunt) then you're done.
The fourth thing is to count with change requests that will come in the middle of your work. That tends to screw up everything, so you need to learn to fight it. How? By asking questions (that was the first thing) and by making a specs. Write everything down, send it in mail (not Skype, Viber, Slack, iMessage or w/e you use for fast communication) and ask your client to approve it and sign it off as a final version. If he comes later and says he wants to add something, you should:
And that is about it :)