Sometimes users (you?) use very common passwords (like *2*4, qwert, 'password') etc. There are huge lists of the most popular passwords, so someone writes a program that tries to login to your account using all of those popular passwords.
Solution: Use strong/uncommon/long passwords

The other way it can happen is if you use the same password at multiple sites. Maybe you have a strong password and use the same one at your grocery store account and also your Yahoo account. If the grocery store gets hacked, now the hackers might have your yahoo password.
Solution: Don't use the same password at multiple sites

Another very popular way is to try and get you to download free applications (usually free games). These often come with spy programs that watch you login, and then send your username and password to the hackers.
Solution: Have an anti-virus program installed and running. Many operating systems now have some sort of "security center" where they will tell you what they recommend to make your computer safer.