I am confused that some people use ESP8266WiFi.h library and some people use AT commands for connecting a WiFi module to Arduino.
Do they work the same or not?
Is there is any problem with the library, or is the library for some different purpose and AT commands for other purpose?
That's a great question. It's definitely not obvious from much of the code and many of the questions people post.
When the ESP8266 first came out, it was only used to connect other processors to Wifi. It shipped running software that provided TCP/IP and Wifi support. You'd wire it up to an Arduino or other processor and control it with AT commands. But you would not change the software that ran on the ESP8266 itself.
Many people still use it this way, but it turns out that the ESP8266 is as capable as (or more capable than) some of the processors that used it to connected to Wifi. So developers put together toolchains for building software to run on the ESP8266 itself. They even ported the Arduino SDK so that you could run "Arduino" programs on it (even though it's not an Arduino).
ESP8266Wifi.h is a header file intended for software that's built to run on the ESP8266, as opposed to software built to run on an Arduino or other processor that uses the ESP8266 to connect to Wifi. When you build software to run directly on the ESP8266, it replaces the software that implements the AT commands - though you can reinstall that later if you want.
So when you see a project using ESP8266.h, it's meant to build for and run directly on the ESP8266. When you see a project using AT commands, it's meant to run on a second processor (most likely an Arduino) that uses the ESP8266 for communications.
There are also libraries like https://github.com/itead/ITEADLIB_Arduino_WeeESP8266 which help an Arduino talk to an ESP8266 - they hide the AT commands and provide a simple set of function calls to do what the AT commands do. This runs on the Arduino, the AT commands are still used but the programmer doesn't have to worry about them.
There are also other languages like LUA and Python that run on the ESP8266 itself. Again, these replace the AT software that it first came with.