If you are a student pilot, you cannot log PIC unless you are solo.
If you are a private pilot or greater, you can log PIC when you are with an instructor.
Assuming you are rated for that Category and Class.
§ 61.51
(e)
Logging pilot-in-command flight time. (1) A sport, recreational, private, or commercial pilot may log pilot-in-command time only for that flight time during which that person—
(i) Is the sole manipulator of the controls of an aircraft for which the pilot is rated or has privileges;
(ii) Is the sole occupant of the aircraft; or
(iii) Except for a recreational pilot, is acting as pilot in command of an aircraft on which more than one pilot is required under the type certification of the aircraft or the regulations under which the flight is conducted.
(2) An airline transport pilot may log as pilot-in-command time all of the flight time while acting as pilot-in-command of an operation requiring an airline transport pilot certificate.
(3) An authorized instructor may log as pilot-in-command time all flight time while acting as an authorized instructor.
(4) A student pilot may log pilot-in-command time only when the student pilot—
(i) Is the sole occupant of the aircraft or is performing the duties of pilot of command of an airship requiring more than one pilot flight crewmember;
(ii) Has a current solo flight endorsement as required under §61.87 of this part; and
(iii) Is undergoing training for a pilot certificate or rating.
The key here is the difference between acting and logging PIC. For example, whilst working on your IR you and your instructor can file and fly an IFR flight plan, in IMC, and the whole time you can log PIC, even though the CFII is actually acting CFI. Same deal with logging PIC in a complex, high performance or tailwheel, without requisite endorsements.
Once you have your private, and you are working on additional ratings it is a good idea to actually establish with the CFI who is actually PIC for the flight.