If you plan to spend several days in Istanbul and visit most of the major museums you may want to consider purchasing an Istanbul museum pass. The pass will give you free entry to most of the government-run museums in Istanbul and save you from waiting in long lines to buy tickets. You can buy a museum pass at Aya Sofya or any of the other major Istanbul museums. It is valid for 5 days and costs 220 lira.
If you only visit Aya Sofya, Kariye Museum, and Topkapı Palace (including the harem) this museum pass will pay for itself. The pass gives you free entry into many of the big museums in Istanbul, including Aya Sofya, Topkapı Palace (including the harem), Kariye Museum, and the Istanbul Archaeology Museum. It will also get you a discount on the Bosphorus tour and discounts at some other private museums, restaurants, and activities.
Note that if you are visiting cities and sites in Turkey other than Istanbul, the Museum Pass Turkey may be a better deal.