Respiration involves Inspiration and Expiration which are accompanied by the alternate increase and decrease of the volume of thoracic cavity. 

Inspiration is an active process and is achieved by increase in all diameters of thoracic cavity. (Thoracic cavity has three diameters – vertical, transverse and anteroposterior.)

The muscles involved in inspiration are:

Diaphragm is the primary muscle of inspiration.It increases all the three diameters of thorax.
 When the diaphragm contracts in inspiration, initially the lower ribs are fixed and the dome of the diaphragm descends, thus increasing the vertical diameter of thorax.

At certain stage, its descent ceases (due to limitation of bulging of anterior abdominal wall and resistance from abdominal viscera like liver) and its central tendon becomes fixed. On further contraction from the fixed central tendon, the lower ribs are elevated, thus increasing anteroposterior and transverse diameter of thorax.

Attachment of Diaphragm:

Origin: Xiphoid process (posterior surface), lower six ribs and their costal ccartilage (inner surface) and upper three lumbar vertebra as right crus and upper two lumbar vertebra as left crus.

Insertion: central tendon

Nerve Supply: Motor nerve supply by Phrenic nerve (C3 C4 C5) and sensory supply by phrenic nerve to centrarl tendon and lower 6 or 7 intercostal nerve to peripheral parts.

Intercostal muscles:
With the first ribs fixed by neck muscles (scalene muscles and sternocleidomastoid muscle), the intercostals muscles (especially external intercostal muscles and interchondral part of internal intercostal muscles of opposite side), elevate the 2nd to 12th ribs and thus act as inspiratory muscles.
If, on the other hand, 12th rib is fixed by quadratus lumborum and oblique muscles of anterior abdominal wall, the intercostal muscles (especially internal intercostal) lower the 1st to 11th ribs, as in expiration.  However, quiet expiration is a passive process achieved by the elastic recoil of the lungs, the relaxation of intercostal muscles and diaphragm and an increased in tone of anterior abdominal wall muscles, which forces the relaxing diaphragm upward.

They are three types: External intercostal muscles, internal intercostal muscles and innermost intercostal muscles.

External intercostal muscles:

Origin: inferior border of rib above and

Insertion: superior border of rib below
These muscles occupy the intercostal space (space between two ribs) except anteriorly from costochondral junction (rib and costal cartilage junction) to sterna margin where it is replaced by anterior intercostal membrane. The muscle fibers run downward and medially in the anterior part and downward and laterally in the posterior part.

Internal intercostal muscles:

Origin: from the costal groove (lower part of inner surface of rib near the inferior border) of the rib above and

Insertion: upper border of rib below
These muscles run deep to external muscles and are replaced by posterior intercostal membrane from angle of the rib to the vertebral end. The muscle fibers are directed at right angle to those of the externus muscles.

Innermost intercostal muscles: It is an incomplete muscle layer and crosses more than one intercostal space. These muscles assist in the function of external and internal intercostal muscles.

Origin: from the costal groove of the rib above and

Insertion: the superior border of rib below

Nerve supply: all the intercostal muscles are supplied by intercostal nerves

How elevation of rib increases the thoracic diameter?
The ribs are attached to vertebra behind. From there, they move forward and downward and attach to sternum in front. The ribs are thus directed forward and downward obliquely. So the elevation of ribs at their sternal ends (primarily 2nd through 6th) results in forward thrust of sternum, thus increasing the anteroposterior diameter of thorax (Pump handle movement).

On the other hand, elevation of these obliquely placed ribs also raises their middle part or lateral most part (is an active process in the 7th to 10th ribs), thus increasing the transverse diameter (bucket handle movement).

When patients with respiratory problems struggle to breath, they use their accessory respiratory muscles to assist the expansion of thoracic cavities. They lean on a table or put their hands on the knees to fix their scapulae and clavicles, so these muscles are able to act on their rib attachments and expand the thorax.

Accessory muscles involved in forced inspiration are
Pectoralis major and minor, Serratus anterior, Scalene group of muscles and sternocleidomastoid
By elevation of first and second ribs by scalene muscles which is otherwise fixed in quiet respiration, by elevation of clavicle by sternocleidomastoid  and other muscles also elevate the ribs, thus help expanding the thoracic cavity and ultimately in inspiration.

Accessory muscles involved in forced expiration are
Muscles of anterior abdominal wall, quadratus lumborum, latissimus dorsi and serratus posterior inferior
Flat muscles of anterior abdominal wall compress the lower part of thorax and increased the intra-abdominal pressure whereas quadratus lumborum fixes the 12th rib Latissimus dorsi and serratus posterior inferior help in forced expiration by depressing the ribs.

Following resources are used while preparing this post (readers are strongly recommended to go through them for more details):
Gray's Anatomy
K. L. Moore's Clinically Oriented Anatomy
R. Snell's Clinical Anatomy